我想要一个字符串拆分并在C#中取其第二个到最后一个 [英] i want a String split and take its 2nd to last in C#

查看:724
本文介绍了我想要一个字符串拆分并在C#中取其第二个到最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的字符串a =A0001。

如何获得值0001



提前谢谢

解决方案

具体方法取决于您的数据格式。如果它总是一个字符,然后是四个数字,那么它是微不足道的:

  string  inp =   A0001; 
string alpha = inp.Substring( 0 1 );
string numeric = inp.Substring( 1 );

另一方面,如果它是一个可变长度前面是非数字的,那么你最好使用正则表达式:

  string  inp =   A0001; 
Regex regex = new 正则表达式( @ (?< Alpha> [^ \d] *)(?<数字> \d +));
匹配m = regex.Match(inp);
string alpha = m.Groups [ Alpha]。值;
string numeric = m.Groups [ 数字]。值;


请尝试下一步:

 < span class =code-keyword> string  a =   A0001; 
char [] array = a.ToCharArray();
var accumulator = new StringBuilder();
foreac(c in 数组)
{
if (< span class =code-keyword> char .IsDigit(c))
accumulator.Append(c);
}
string result = accumulator.ToString();


  string  a =   A0001\" ; 
a = a.split(' A')[ 0 ]的ToString();


Hi all,
my string a="A0001".
How i get the value "0001"

Thanks in advance

解决方案

Exactly how you do this will depend on what your data format is like. If it is always "one character, followed by four numerics, then it is trivial:

string inp = "A0001";
string alpha = inp.Substring(0,1);
string numeric = inp.Substring(1);

If on the other hand it is a variable length non-numeric at the front, then you would be better off using a Regex:

string inp = "A0001";
Regex regex = new Regex(@"(?<Alpha>[^\d]*)(?<Numeric>\d+)");
Match m = regex.Match(inp);
string alpha = m.Groups["Alpha"].Value;
string numeric = m.Groups["Numeric"].Value;


Hi , try next:

string a="A0001";
char[] array=a.ToCharArray();
var accumulator=new StringBuilder();
foreac(c in array)
{
if(char.IsDigit(c))
accumulator.Append(c);
}
string result =accumulator.ToString();


string a="A0001";
a = a.split('A')[0].ToString();


这篇关于我想要一个字符串拆分并在C#中取其第二个到最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆