# String.ReplaceAt

**URL:** <https://forum.xojo.com/t/string-replaceat/53895>\
**Category:** General\
**Created:** [May 18, 2020, 5:33pm UTC](https://forum.xojo.com/t/string-replaceat/53895 "2020-05-18T17:33:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dale\_Arends](https://forum.xojo.com/user_avatar/forum.xojo.com/dale_arends/32/442_2.png) [@Dale\_Arends](https://forum.xojo.com/u/Dale_Arends)\
**Post date:** [May 18, 2020, 5:33pm UTC](https://forum.xojo.com/t/string-replaceat/53895/1 "2020-05-18T17:33:46Z")

</div>

I have a need to replace a portion of a string with another string when there may be more than one instance of the target string in the original string. For example, I may have a string like “The fourth hour of the fourth day of the fourth month of the fourth season…” and I want to replace the second “fourth” with “fifth” so that the resulting string is “The fourth hour of the fifth day of the fourth month of the fourth season…” Currently, I have to laboriously parse and separate the string and rebuild it.

With all of the String.Replace… methods in Xojo, what I would really like is a String.ReplaceAt method that takes a ‘findString’, a ‘replaceString’, and an ‘instance’ integer.

---

<div class="post-metadata">

**Author:** ![Stefan\_Cyrus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@Stefan\_Cyrus](https://forum.xojo.com/u/Stefan_Cyrus)\
**Post date:** [May 18, 2020, 6:10pm UTC](https://forum.xojo.com/t/string-replaceat/53895/2 "2020-05-18T18:10:47Z")

</div>

Split the string by “fourth”  
rebuld the string

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 18, 2020, 7:16pm UTC](https://forum.xojo.com/t/string-replaceat/53895/3 "2020-05-18T19:16:40Z")

</div>

```auto
dim rx as new RegEx
rx.SearchPattern = "(?Umsi)((?:.*fourth){1})(.*)fourth"
rx.ReplacementPattern = "$1$2fifth"

dim replacedText as string = rx.Replace( sourceText )
```

The {1} says, “skip the first occurrence”, so if you want to change the x occurrence, change that to x-1.

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [May 18, 2020, 7:17pm UTC](https://forum.xojo.com/t/string-replaceat/53895/4 "2020-05-18T19:17:29Z")

</div>

Another option is RegEx. The [RegEx.Replace](https://documentation.xojo.com/api/text/regular_expressions/regex.html#regex-replace) page has sample code that does this:

[code]Var re As New RegEx  
re.SearchPattern = “\bfourth\b”  
re.ReplacementPattern = “fifth”

Var sampleText As String = “The fourth hour of the fourth day of the fourth month of the fourth season…”

Var match As RegExMatch = re.Search(sampleText) // find first SearchPattern in text  
If match \<\> Nil Then  
sampleText = re.Replace // Find the next SearchPattern in the text and replace it  
End If[/code]

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [May 18, 2020, 7:34pm UTC](https://forum.xojo.com/t/string-replaceat/53895/5 "2020-05-18T19:34:24Z")

</div>

missing is Mid(“Hello”,1,1)=“ABC”  
it was useful in VB6

---

<div class="post-metadata">

**Author:** ![Dale\_Arends](https://forum.xojo.com/user_avatar/forum.xojo.com/dale_arends/32/442_2.png) [@Dale\_Arends](https://forum.xojo.com/u/Dale_Arends)\
**Post date:** [May 19, 2020, 4:22pm UTC](https://forum.xojo.com/t/string-replaceat/53895/6 "2020-05-19T16:22:24Z")

</div>

[quote=489156:@Kem Tekinay][code]  
dim rx as new RegEx  
rx.SearchPattern = “(?Umsi)((?:._fourth){1})(._)fourth”  
rx.ReplacementPattern = “$1$2fifth”

dim replacedText as string = rx.Replace( sourceText )  
[/code]

The {1} says, “skip the first occurrence”, so if you want to change the x occurrence, change that to x-1.[/quote]  
Very nice. Not the approach I used, Kem, since I am not very adept at RegEx but it is definitely going into my tools module.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [April 29, 2021, 4:25pm UTC](https://forum.xojo.com/t/string-replaceat/53895/7 "2021-04-29T16:25:08Z")

</div>


