[英]C# How can i split a string correctly?
((100&12)%41)&(43&144)
this is my string and i want to split this string like this: ((100&12)%41)&(43&144)
这是我的字符串,我想像这样分割字符串:
(
(
100
&
12
41
)....
I try using string.ToCharArray() method but big integer numbers brings a problem like 100: 我尝试使用string.ToCharArray()方法,但是大整数会带来类似100的问题:
1
0
0
thanks for help 感谢帮助
如果您进一步处理数组以连接连续的char.isdigit()字符,或者无法使用快速的1命令解决方案imo,string.ToCharArray()可以工作,或者可以通过配置使用String.split方法提取100、12和41个块您正确地分隔,并用string.ToCharArray分割其余部分。
try iterating through you string and watch if next char is a digit. 尝试遍历您的字符串,并观察下一个字符是否为数字。 If false then split, else skip
如果为false,则拆分,否则跳过
You can write your own tokenizer, but for help is usefull to have sth like this(code is not tested): 您可以编写自己的令牌生成器,但对于这样的帮助(有用的代码未经测试)非常有用:
var lst = new List<string>();
for (int i=0;i<str.Length;i++)
{
if (char.IsDigit(str[i])
{
var tmp = new string(new []{str[i]});
i++;
while(i<str.Length && char.IsDigit(str[i]))
{ tmp+= str[i]; i++}
i--;
lst.Add(tmp);
}
else
lst .Add(new string(new []{str[i]}));
}
It returns the list of lines: 它返回行列表:
static List<string> SplitLine(string str)
{
var lines = new List<string>();
for (int i = 0; i < str.Length; i++)
{
if (!char.IsDigit(str[i]))
{
lines.Add(str[i].ToString());
continue;
}
string digit = "";
while (char.IsDigit(str[i]))
{
digit += str[i++];
}
i--;
lines.Add(digit);
}
return lines;
}
Output: 输出:
(
(
100
&
12
)
%
41
)
&
(
43
&
144
)
using System;
using System.Text.RegularExpressions;
class Sample {
public static void Main(){
var str = "((100&12)%41)&(43&144)";
var pat = "([()&%])";
var tokens = Regex.Split(str,pat);
foreach(var token in tokens){
if( !string.IsNullOrEmpty(token))
Console.WriteLine("{0}", token);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.