如何拆分非数字作为分隔符的字符串?

时间:2015-08-24 11:16:09

标签: c# asp.net split

我希望在while : do for name in /tmp/?????-Stock.txt do [ -f "$name" ] && break 2 done sleep 2 done split一个字符串。它应该基于字符串中的文本C#。我有一个字符串split,我希望"41sugar1100"基于split text { {1}}。我怎么能这样做?

注意:不直接将"sugar"作为"sugar"传递。因为文本可以在下一次迭代中更改。只要在字符串中找到文本,它就应该根据该文本进行拆分。 / p>

6 个答案:

答案 0 :(得分:10)

使用Regex.Split

string input = "44sugar1100";
string pattern = "[a-zA-Z]+";            // Split on any group of letters

string[] substrings = Regex.Split(input, pattern);
foreach (string match in substrings)
{
    Console.WriteLine("'{0}'", match);
}

答案 1 :(得分:1)

char[] array = "41sugar1100".ToCharArray();
StringBuilder sb = new StringBuilder();

// Append letters and special char '#' when original char is a number to split later
foreach (char c in array)
    sb.Append(Char.IsNumber(c) ? c : '#');

// Split on special char '#' and remove empty string items
string[] items = sb.ToString().Split('#').Where(s => s != string.Empty).ToArray();

foreach (string item in items)
    Console.WriteLine(item);

// Output:  
// 41  
// 1100  

答案 2 :(得分:0)

****使用char []数组从字符串****中分割字符串

 string s = "44sugar1100";
        char[] c = new char[] { 's', 'u', 'g', 'a', 'r' };
        string[] s1 = s.Split(c,StringSplitOptions.RemoveEmptyEntries);
        string s2 = s1.ToString();

答案 3 :(得分:0)

Regex regex = new Regex(@"(?<firstNumber>\d+)(?<word>[^\d]+)+(?<secondNumber>\d+)", RegexOptions.CultureInvariant);

string s = "41sugar1100";
Match match = regex.Match(s);

        if (match.Success)
        {
            string firstNumber = match.Groups["firstNumber"].Value;
            string word = match.Groups["word"].Value;
            string secondNumber = match.Groups["secondNumber"].Value;
        }

答案 4 :(得分:0)

我会将字符串放入char数组中 然后int.tryparse数组中的每个char例如......

string myString = "44sugar1100";
int num=0; //for storage
string newString="";//for rebuilding
foreach(char ch in myString)
{
    if(int.TryParse(ch, out num)
    {
    newString+=num.toString();
    }
}

答案 5 :(得分:-3)

p