.Net Delete all first 0 lines

I got the following:

05/01/03

I need to convert this value to 1.5.3

The problem is that I can not only trim 0, because if I got:

05/01/10

I need to convert this value to 1.5.10

So what is the best way to solve this problem? Regex? If so, any regular expression example that does this?

+7
c # regex
source share
8 answers

@FrustratedWithFormsDesigner answer extension:

string Strip0s(string s) { return string.Join<int>(".", from x in s.Split('.') select int.Parse(x)); } 
+16
source share

Regex-replace

 (?<=^|\.)0+ 

with an empty string. Regular expression:

 (? <= # begin positive look-behind (ie "a position preceded by")
   ^ | \.  # the start of the string or a literal dot †
 ) # end positive look-behind
 0+ # one or more "0" characters

† Note that not all regular expression flavors support a variable-length look, but .NET does.

If you expect this type of input: "00.03.03" and want to keep the leading zero in this case (for example, "0.3.3" ), use this expression:

 (?<=^|\.)0+(?=\d) 

and replace the empty string again.


From the comments (thanks to Kobi): There is a more concise expression that does not require appearance and is equivalent to my second sentence:

 \b0+(?=\d) 

which the

 \ b # a word boundary (a position between a word char and a non-word char) 
 0+ # one or more "0" characters
 (? = \ d) # positive look-ahead: a position that followed by a digit

This works because 0 is a word character, so word boundaries can be used to find the first 0 in a string. This is a more compatible expression because many regex flavors do not support variable-length looks, and some (like JavaScript) do not look at all.

+15
source share

You can split the string in . and then trim the leading 0 according to the split results, then combine them back.

I don’t know how to do it in one operation, but you can write a function that hides it and makes it look like a single operation .;)

UPDATE:

I didn’t even think about another reggae. Yes, it will probably do it in one operation.

+3
source share

Here's another way you can do what FrustratedWithFormsDesigner offers:

 string s = "01.05.10"; string s2 = string.Join( ".", s.Split('.') .Select(str => str.TrimStart('0')) .ToArray() ); 

This is almost the same as dtb's answer, but does not require the substrings to be real integers (it would also work, for example, "000A.007.0HHIMARK").

UPDATE If you want any lines consisting of all 0 in the input line to be output as one 0, you could use this:

 string s2 = string.Join( ".", s.Split('.') .Select(str => TrimLeadingZeros(str)) .ToArray() ); public static string TrimLeadingZeros(string text) { int number; if (int.TryParse(text, out number)) return number.ToString(); else return text.TrimStart('0'); } 

I / O Example:

 00.00.000A.007.0HHIMARK // input 0.0.A.7.HHIMARK // output 
+1
source share

There is also an old-school path that probably has better performance characteristics than most of the other solutions mentioned. Something like:

 static public string NormalizeVersionString(string versionString) { if(versionString == null) throw new NullArgumentException("versionString"); bool insideNumber = false; StringBuilder sb = new StringBuilder(versionString.Length); foreach(char c in versionString) { if(c == '.') { sb.Append('.'); insideNumber = false; } else if(c >= '1' && c <= '9') { sb.Append(c); insideNumber = true; } else if(c == '0') { if(insideNumber) sb.Append('0'); } } return sb.ToString(); } 
+1
source share
 string s = "01.05.10"; string newS = s.Replace(".0", "."); newS = newS.StartsWith("0") ? newS.Substring(1, newS.Length - 1) : newS; Console.WriteLine(newS); 

NOTE. You need to carefully check the possible combination of input.

0
source share

It looks like it's a date format, so I would use a date processing code

 DateTime time = DateTime.Parse("01.02.03"); String newFormat = time.ToString("dMyy"); 

or even better

 String newFormat = time.ToShortDateString(); 

who will respect you and your culture settings of your customers.

If this data is not a date then do not use it :)

0
source share

I had a similar requirement to parse a string with street addresses where some house numbers had zero values, and I had to delete them, leaving the rest of the text intact, so I slightly edited the accepted answer to meet my requirements, maybe someone find it useful. Basically, doing the same thing as the accepted answer, with the difference that I check if the string can be parsed as an integer, and by default for the string value when false;

 string Strip0s(string s) { int outputValue; return string.Join(" ", from x in s.Split(new[] { ' ' }) select int.TryParse(x, out outputValue) ? outputValue.ToString() : x); } 

Entrance: "Brygga Islands 34 B 07 TV"

Conclusion: "Brygge Islands 34 B 7 TV"

0
source share

All Articles