Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! Please help me with this. I want to do two things in a string.
First, for example, the value of the string is "file.name.txt", I'd like to change it to "file.name_txt". If ever there are multiple ".", I'd like to change the last "." to "_" and retain the preceding ".".
Secondly, If I have a string value "file.name_txt.enc", I like to replace the last "_" to "." and remove the characters starting from the last "."
I'm familiar with the replace and remove methods of string but i don't know how to do it when there are repetition of the specific character that I want to replace or remove.
Posted
Comments
[no name] 17-Aug-14 8:17am    
Then you need to use IndexOf.

1 solution

Use string.LastIndexOf to ID the final 'dot' and then string.Substring to change it:
VB
Dim filename As String = "file.name.txt"
Dim lastDot As Integer = filename.LastIndexOf("."C)
filename = filename.Substring(0, lastDot) & "_" & filename.Substring(lastDot + 1)

You can use similar code to do the second task.
 
Share this answer
 
Comments
tony_01 17-Aug-14 9:09am    
It worked! Thank you very much @OriginalGriff. You're a big help!
OriginalGriff 17-Aug-14 9:35am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900