0

I'm trying to replace the "00" that might come from a phone number when a user enters the number. And of course I only want the first 00 to get replaced with a + sign. In Java there is the method:

result.replaceFirst("00", "+");

Is there anything like that in .NET? Or is there any smart way to do this in vb.NET?

PaperThick
  • 2,749
  • 4
  • 24
  • 42
  • Surely you only want to do that replacement if the string *starts* with "00" - not just wherever the first occurrence of these digits appear in the string? – Damien_The_Unbeliever Aug 16 '12 at 10:13
  • 1
    http://stackoverflow.com/questions/141045/how-do-i-replace-the-first-instance-of-a-string-in-net – Maarten Aug 16 '12 at 10:22

2 Answers2

1

Try this:

var clean = text.StartsWith("00") ? "+" + text.Substring(2) : text;
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

Enigmativity's is a cool 1 liner. You can also try this..

    if(result.StartsWith("00"))
       result= result.Replace(result.Substring(0, 2), "+");
user841311
  • 597
  • 3
  • 9
  • 24