Replace a string character at speciafic location

I have a need/desire to replace string characters at specific locations. I cannot use the replacement functions I have found so far because they are based on character rather than location. Further, the character I want to replace may appear more than once in a string and I want to replace only a specific instance of that character. I may not “know” what the current character is, though I think I can go through a number of program steps to find it. All in all, a function that says “replace the block of characters beginning at character N and with length L with the specified replacement block”.woluld be perfect. Is there something I have overlooked that will do something like this? I do understand that encodings make this challenging but I go to great lengths to (try to) insure that everything is ASCII or compatible with ASCII.

Lament: it is dead easy in C since strings are, themselves. arrays and you only need an index to access, replace, whatever, any character.

Many thanks - Jim Wagner, Oregon Research Electronics

Use a MemoryBlock:

Dim s As MemoryBlock = "Hello"
s.StringValue(3, 1) = "L" ' "HelLo"

Ahhh, thank you! Jim

It is obligatory to point out that this technique is ONLY valid if you know that your data is ASCII only. If your data is a string with characters outside that range, you run the risk of damaging the string data.

1 Like

Completely understand. If not ASCII, then you no loinager have 1:1 relationship between characters and single bytes. - Jim

you could later search the part you will replace by offset
x=mystring.IndexOf(startPosition, searchString, …
if x in your range
you take the left part without the first char of the search (-1) .Middle or .Left
you take the right part after the search word by .Middle or .Right
then you put it together
new string = left part + new word + right part

btw. the good old MS VB 6 had a Mid$(“Any Text”, 1, 3) = “XY”