[英]Substring until find the number
I have a string LCD32W58
. 我有一个字符串
LCD32W58
。 In this string I want substring until I get number 在此字符串中,我需要子字符串,直到获得数字
eg LCD32W58 --> LCD
例如
LCD32W58 --> LCD
If the string contains numeric at beginning then I want numbers until character start 如果字符串开头包含数字,则我希望数字直到字符开始
eg 55LM9600 --? 55
例如
55LM9600 --? 55
55LM9600 --? 55
you can use something like this 你可以用这样的东西
if @nString like '[0-9]%'
select @Result = left(@nString, patindex('%[A-Za-z]%', @nString) - 1)
else if @nString like '[A-Za-z]%'
select @Result = left(@nString, patindex('%[0-9]%', @nString) - 1)
you can also use numbers
and not numbers
style 您也可以使用
numbers
而not numbers
样式
if @nString like '[0-9]%'
select @Result = left(@nString, patindex('%[^0-9]%', @nString) - 1)
else
select @Result = left(@nString, patindex('%[0-9]%', @nString) - 1)
The methode wroks only if you have just numbers and letters in your string 仅当字符串中只有数字和字母时,方法才会崩溃
private string ParseString(string s)
{
int i = 0;
int j = 0;
foreach (char c in s)
{
if (isAlphanummeric(c)
{
if (j > 0)
break;
i++;
}
else if(isNummeric(c)
{
if (i > 0)
break;
j++;
}
else
{
throw new NotSupportedException(
"Only letters and numbers are expected.");
}
}
return (s.Substring(0, (i > 0 ? i: j)));
}
private bool isAlphanummeric(Char decValue)
{
return ((decValue > 64) && (decValue < 91)) ||
((decValue > 98) && (decValue < 123));
}
private bool isNummeric(Char decValue)
{
return ((decValue > 47) && (decValue < 58));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.