Insert string in string at position n

Is there function to insert a string in another string at position n?
e.g. how can I change this 1234567 to 123 4567
insert a space at position 4

I can do it like this…
Dim MyArray() as string
MyArray = Split(TextField1.Text, “”)

Dim TheFormattedString as String

TheFormattedString = TextField1.Text.Left(3) + " "

For i as integer = 3 to UBound(MyArray)
TheFormattedString = TheFormattedString + MyArray(i)
next i

msgbox TheFormattedString

Is there an elegant way to do it?

Thanks.

Lennox

Probably your next post will be 'ah but what I REALLY want to do is…"

Anyway…

[code] dim s , t as string
s = “12345678”

t = replace(s,“34”,“3 4”)
//or
t = left(s,3) + " " + mid(s,4)[/code]

Thanks Jeff

dim s , t as string
t = left(s,3) + " " + mid(s,4) looks good.

Thanks again.

Lennox

For numbers you can use the format function

Format(1234567, "000\\ 0000")

Thanks Wayne,

May also come in useful.

Thanks again.
Lennox

If you work with the Text datatype you can also do

  dim t as Text  ="1234567"
  dim chars() as Text = t.Split
  chars.Insert(3, " ")
// do what ever you like
t = Text.join(t, "")

A quick function which will give you the flexibility to insert anything in your string at any position:

Function InsertStringInString(insert as String, inString as string, position as integer) As String Return Mid(inString,1,position) + insert + Mid(inString,position + 1,Len(inString)-position) End Function

Thanks for all the responses.
Lennox

Hi Rob,

Couldn’t get …
dim t as Text =“1234567”
dim chars() as Text = t.Split
chars.Insert(3, " ")
// do what ever you like
t = Text.join(t, “”)

to work, but this worked…
dim t as Text
t = TextField1.Text.toText
dim chars() as Text = t.Split
chars.Insert(3, " ")
// do what ever you like
t = Text.join(chars(), “”)
'msgbox t
TextField1.Text = t

Thanks again.

Lennox