Tuesday, July 29, 2014

How can i remove/delete digits in the beginning of a string ?

The problem is that in tn.Text the string start with a number and a dot/point for example:



1. hello

2. hi

3. world



And i need that tn.Text will contain only the text for example:



hello

hi

world

Your example has a space after each dot. Is it always there?



Is that supposed to be part of the string you want?



string s1 = "1. hello";
string s1a = s1.Substring(s1.IndexOf('.') + 1); // Gives s1a as " hello"

If you want to remove the leading space(s):



s1a = s1a.TrimStart(" ".ToCharArray());

Or you can just use TrimStart to remove the numbers, dot and any spaces:



// Gives s2a as "hello"
string s2 = "1. hello";
string s2a = s2.TrimStart("0123456789. ".ToCharArray());

- Wayne


No comments:

Post a Comment