[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

how to ignore strings starting with numbers

lets say i have a few strings

Welcome
test
123bat
ball456

and I have a foreach looking at all the strings
how can i quickly test if the first char in the string is a number

output should look like

Welcome
test
ball456
0
no158
Asked:
no158
1 Solution
 
DhaestCommented:
string strToTest = "123";
Regex reNum = new Regex(@"^\d+$");
(or regex(@"^\d{5}$");
bool isNumeric = reNum.Match(strToTest).Success

http://www.codeproject.com/KB/cs/csharp-isnumeric.aspx
0
 
Praveen KumarArchitectCommented:
......

int a;

if(Int32.TryParse(yourstring.SubString(0,1), out a) == true)
    MessageBox.Show("Yes, this is start with number");

....

0
 
no158Author Commented:
Tried using the regular expression first with this as data

123first test
second123 test
this123third test
123

all of them went through, as if there was no test

Secondly i tried the Int32.TryParse but it seems that my string wont allow substrings


here is what i'm working with


                            foreach (string s in myString)
                            {
                                if (!(startString.Contains(s)))
                                {
                                    Regex reNum = new Regex(@"^\d{5}$");
                                    bool isNumeric = reNum.Match(s).Success;
                                    if (!isNumeric)
                                    {
                                        myString[count] = s;
                                        list = new KeyValueList(myString[count], lineTemp, index);
                                        myList.Add(list);
                                        count++;
                                    }
                                }
                                index++;
                            }

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
DhaestCommented:
Try it with 1 instead of 5
Regex reNum = new Regex(@"^\d{1}$");
0
 
no158Author Commented:
Didn't work either.

On second thought, it would be better if it would just not accept any string with a digit in it.
If this makes it easier.

So all these would fail the test.

123first
second123
this123third
123
0
 
ColemssCommented:
internal static bool IsNumeric(string numberString)
{
    foreach (char c in numberString)
   {
        if (!char.IsNumber(c))
             return false;
   }
   return true;
}
0
 
DhaestCommented:
>> On second thought, it would be better if it would just not accept any string with a digit in it.
If this makes it easier.

Does the user needs to input this in a textbox ? Then I would also accept only numeric values, ...
0
 
no158Author Commented:
internal static bool IsNumeric(string numberString)
{
    foreach (char c in numberString)
   {
        if (!char.IsNumber(c))
             return false;
   }
   return true;
}

worked perfect for what I was doing. Only thing was I used IsDitgit.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now