Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C#中的字符串中删除初始数字字符?_C#_Regex - Fatal编程技术网

如何从C#中的字符串中删除初始数字字符?

如何从C#中的字符串中删除初始数字字符?,c#,regex,C#,Regex,如何使用正则表达式从字符串中删除第一个数字字符? 字符串str=20Be45 输出->Be45这是正则表达式 string text = "20Be45"; string replaced = Regex.Replace(text, "^[0-9]+", string.Empty); 但是,我编程了15年没有使用正则表达式,你知道吗?我很快乐,我的程序也很有效 int i = 0; while (i < text.Length && text[i] >= '0'

如何使用正则表达式从字符串中删除第一个数字字符? 字符串str=20Be45

输出->Be45

这是正则表达式

string text = "20Be45";
string replaced = Regex.Replace(text, "^[0-9]+", string.Empty);
但是,我编程了15年没有使用正则表达式,你知道吗?我很快乐,我的程序也很有效

int i = 0;

while (i < text.Length && text[i] >= '0' && text[i] <= '9')
{
    i++;
}

text = text.Substring(i);
inti=0;

而(i='0'&&text[i]这是使用以下方法的另一个解决方案:


另一个非正则表达式版本:

var text = "20Be45";
var result = string.Concat(text.SkipWhile(char.IsDigit));

您还删除了第二个数字字符。是的,这就是我要找的。(初始数字字符)char.IsNumber或char.IsDigit?@jamesbarras
char.IsDigit
可能更好…但是我已经很老了,所以我仍然使用>='0';)对我来说太老了,我的编程年限和你差不多。。。正则表达式是处理这类事情的更好方法。@ZoharPeled C没有包含regexp。。。C++直到几年前才出现。VB6没有。然后还有另一个“分支”(Perl/PHP/Python),这是我对这个问题的首选解决方案。
var text = "20Be45";
var result = string.Concat(text.SkipWhile(char.IsDigit));