如何在字符串中只找到第一个和最后一个数字字符?
例如:字符串 a = 1234 查找结果:14,但不是 1234 或 23
它用于格式化字符串。并从文件加载。所以只有正则表达式
加载的正则表达式解析器:
Match resultMatch = new Regex(regex).Match(sourceString);
return resultMatch.Success ? resultMatch.Value : null;
我不太了解 C#,所以这里有一些或多或少的伪代码:
char first;
char last;
int i;
for (i = 0; i < a.Length; i++)
{
if (a[i] matches "[A-Za-z0-9]")
{
first = a[i];
break;
}
}
int j;
for (j = a.Length - 1; j > i; j--)
{
if (a[j] matches "[A-Za-z0-9]")
{
last = a[j];
break;
}
}
return first + last;