Replace a character in the string

Hi,

I have a String with value = 1.241.82

I want to replace only the first period (.) in the string with comma (,).
The first period can come anywhere in the string (like 12.41.82 or 124.1.82)
The index of the first period is dynamic.
I tried the below approach, but it did not work.

Any suggestion would be helpful
Thanks :slight_smile:

Hi @Techno1
please go throuh the below image
variable “idx” is of type int32

@Techno1

Please try this

str.Substring(0,str.IndexOf(".")) + "," + str.Substring(str.IndexOf(".")+1)

here str is the string variable where the original string is stored

image

Hope this helps

cheers

Hi @Techno1

Try this:

Input="1.241.82"
Output= Input.Substring(0, Input.IndexOf(".")) & "," & Input.Substring(Input.IndexOf(".") + 1)

Input="12.41.82"
Output= Input.Substring(0, Input.IndexOf(".")) & "," & Input.Substring(Input.IndexOf(".") + 1)

Hope it helps!!

@Techno1

system.Text.RegularExpressions.Regex.Replace(inputstr,system.Text.RegularExpressions.Regex.Match(inputstr,“\d+.”).Value,system.Text.RegularExpressions.Regex.Match(inputstr,“\d+.”).value.replace(“.”,“,”))

you can also try this

Hi @Techno1 ,

Could check with the below as well :

(new Regex("\.")).Replace("1.241.82",",",1)

image

Hi,

FYI, another approach using Microsoft.VisualBasic.Strings.Replace:

Strings.Replace(yourString,".",",",1,1)

image

Regards,

1 Like