tags:

views:

295

answers:

3

I have a text in this format:

term: 156:59 datainput

I want to remove the ":" between the number and then replace it with something else so the text can become:

term: 156-59 datainput

How can I do this in VB.NET?

A: 

Assuming you have read the data in to a string, take a look at the string.replace function.

benophobia
How do people keep voting up stuff that is obviously wrong to anyone who read the question carefully?
TheTXI
My bad.That will teach me to skim the question.
benophobia
If it makes you feel any better, there were approximately 5 or so others who did the exact same thing.
TheTXI
+5  A: 

Yes, I know this is a little clumsy, but it should work (assuming your data is in exactly the format you specify):

input[input.IndexOf(":", input.IndexOf(":")+1)] = "#"

Of course, if you want a more general case to find NUMBER:NUMBER and replace it with NUMBER#NUMBER, I'd recommend using a regular expression, like this:

var re = new Regex(@"(\d+):(\d+)");
re.Replace(input, "$1#$2");
Jonathan
Finally someone got it
TheTXI
Good job spotting the first ":". I think quite few didn't note that one immediately. Including me.
Mikko Rantanen
@Mikko: I can't take credit for noticing that -- I had a post written up linking to string.replace before I saw another answer that had it, and a comment saying it was wrong.
Jonathan
Whoops... somehow the whole 'VB.Net' thing escaped me... Sorry about that Rithet.
Jonathan
No problem, you still got my vote for the first correct answer even though it's in C#.
Rithet
+6  A: 

In VB.NET (credit Jonathan):

    Dim text As String = "term: 156:59 datainput"
    Dim fixedText As String = Regex.Replace(text, "(\d+):(\d+)", "$1-$2")

nb: removed last two lines as suggested.

Student for Life
Probably not necessary to have the last two lines, but #1 for having the first correct answer in VB version.
TheTXI
Well, This answer works. Thanks
Rithet