如何在c#中拆分字符串 [英] How to split the string in c#
本文介绍了如何在c#中拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我需要拆分字符串。
例如:
q2px1tfi9jNvrX07uxH78DUCzuXZKlwZjtN3iruaMi0 =这是我的字符串。现在我需要拆分
q2px1-tfi9j- NvrX0-7uxH7-8DUCz。并且其余的字符串是不需要的。
hi all,
i need to split the string.
for eg:
"q2px1tfi9jNvrX07uxH78DUCzuXZKlwZjtN3iruaMi0=" This is my string. now i need to split
q2px1-tfi9j- NvrX0-7uxH7-8DUCz. and the remaining strings are no need.
推荐答案
您好,
您可以使用常规字符串执行此操作表达式如下所示。
Hello,
You can do it using a regular expression as shown below.
using System;
using System.Text.RegularExpressions;
class SplitStringTester {
public string[] RegxSplit(string pattern, string input) {
int i = 0;
string[] arrRet = null;
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
arrRet = new string[matches.Count];
foreach(Match m in matches) {
arrRet[i] = m.Value;
i++;
}
}
return arrRet;
}
public static void Main() {
SplitStringTester tst = new SplitStringTester();
string[] arrRet = tst.RegxSplit(@"(.{5})?", @"q2px1tfi9jNvrX07uxH78DUCzuXZKlwZjtN3iruaMi0=");
if (arrRet != null) {
Console.WriteLine("{0} ({1} matches):", @"q2px1tfi9jNvrX07uxH78DUCzuXZKlwZjtN3iruaMi0=", arrRet.Length);
foreach (string sTok in arrRet)
Console.WriteLine(sTok);
}
Console.ReadKey();
}
}
Another way could be found here[^].
Regards,
这篇关于如何在c#中拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文