# How to substitute a string with different size

**URL:** <https://www.ruby-forum.com/t/how-to-substitute-a-string-with-different-size/110849>\
**Category:** Ruby\
**Created:** [July 31, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/how-to-substitute-a-string-with-different-size/110849 "2007-07-31T15:25:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 31, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/how-to-substitute-a-string-with-different-size/110849/1 "2007-07-31T15:25:38Z")

</div>

Hi all,

I deal with a mixture of text and binary file. I want to search a  
pattern in the string and replace it with a new pattern, then write to a  
new file. I use the following script(only part of the script)

str.gsub!(/old\_pattern/,“new\_pattern”)  
file.seek(-str.length,IO::SEEK\_CUR)  
file.print str

It works fine if the both old\_pattern and new\_pattern are the same size.  
But it will produce unpreditable result if the sizes are different. I  
wonder if any one out there knows how to explain it and provide a Ruby  
way solution.

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 31, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/how-to-substitute-a-string-with-different-size/110849/2 "2007-07-31T17:23:04Z")

</div>

Hi Li,

What do you mean ‘unpreditable result’? Do you mean that the size of  
‘str’  
object is not correct?

2007/7/31, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 31, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/how-to-substitute-a-string-with-different-size/110849/3 "2007-07-31T18:10:16Z")

</div>

Yang B. wrote:

> Hi Li,
> 
> What do you mean ‘unpreditable result’? Do you mean that the size of  
> ‘str’  
> object is not correct?
> 
> 2007/7/31, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

Actually what I try to do is to replace one string with another string  
in some FACS data files, in which part of them are headers in text  
format and the rest of them in binary format. For example replace “CD3”  
with “CD4” or “CD45”. If it is the first case the new file is of same  
size and I can open the new file without losing the image information  
but I will lost all the image information if I replace “CD3” with  
“CD45”. This is what I mean by unpreditable result.

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 31, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/how-to-substitute-a-string-with-different-size/110849/4 "2007-07-31T18:39:12Z")

</div>

> Actually what I try to do is to replace one string with another string  
> in some FACS data files, in which part of them are headers in text  
> format and the rest of them in binary format. For example replace “CD3”  
> with “CD4” or “CD45”. If it is the first case the new file is of same  
> size and I can open the new file without losing the image information  
> but I will lost all the image information if I replace “CD3” with  
> “CD45”. This is what I mean by unpreditable result.

It sounds like there’s some sort of index at the beginning of the file  
you’re changing. E.g. at the beginning of the file it says: 3 bytes of  
data, followed by an image. If you substitute “CD3” for “CD45”, you’d  
also have to update the index at the beginning to say “4 bytes of  
data, followed by an image”.

I’m afraid I don’t know FACS, but it sure sounds like the nature of  
the file format doesn’t allow you to just search and replace in the  
file without updating some context information as well.  
-tim

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [August 1, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/how-to-substitute-a-string-with-different-size/110849/5 "2007-08-01T20:12:59Z")

</div>

Tim B. wrote:

> It sounds like there’s some sort of index at the beginning of the file  
> you’re changing. E.g. at the beginning of the file it says: 3 bytes of  
> data, followed by an image. If you substitute “CD3” for “CD45”, you’d  
> also have to update the index at the beginning to say “4 bytes of  
> data, followed by an image”.
> 
> I’m afraid I don’t know FACS, but it sure sounds like the nature of  
> the file format doesn’t allow you to just search and replace in the  
> file without updating some context information as well.  
> -tim

I don’t know FACS either but I ask the technical services and they say  
it is a kind of meta data file. If I insert extra characters into the  
file I have to remove equal extra spaces from somewhere else in the  
file.

Li
