Search for alpha character - STOP WHEN NUMBER FOUND

  • Thread starter Thread starter G
  • Start date Start date
G

G

Hello,

I would like look at a user-entered string, and copy all characters starting
from the left until a number is found. As soon as a number is detected, the
copy should stop and the chracters stored in a second textfield. Sounds
simple but haven't the foggiest how to do it.

For example:

Texfield1.Text = "Bryan2000"
TextField2.Text = "Bryan"

or

TextField1.Text = "H500"
TextField2.text = "H"

or

TextField1.Text = "900TEST"
TextField2.text = ""

Any ideas?

Regards,

G.
 
Any ideas?

I'm sure there are many cleverer and more efficient ways of doing this,
but...

string strNumbers = "0123456789";
string strText1 = "Bryan2000";
string strText2 = String.Empty;

for (int intPos = 0; intPos < strText1.Length; intPos++)
{
if (strNumbers.Contains(strText1.Substring(intPos, 1)))
{
break;
}
else
{
strText2 += strText1.Substring(intPos, 1);
}
}
 
Two ways:

public static string GetNonNumericString1(string input)
{
if (String.IsNullOrEmpty(input))
return String.Empty;

System.Text.RegularExpressions.Match match =
System.Text.RegularExpressions.Regex.Match(input, "[^0-9]+");

return match.Success ? match.Value : input;
}

public static string GetNonNumericString2(string input)
{
if (String.IsNullOrEmpty(input))
return String.Empty;

int position = input.IndexOfAny(
new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});

return position < 0 ? input : input.Substring(0, position);
}
 
Back
Top