简体   繁体   中英

Split string at particular characters (C#)

What I want to do is to split an array and then put the character which i split at into another element
ie
string text = "1*5+89-43&99"
should become
string[] textsplit = ["1","*","5","+","89","-","43","&","99"] (it must be a string) and I will supply the characters to be left in seperate elements

You can do this using string.IndexOfAny .

Simply keep looking for the next index of any of the separators. When you find a separator, add the text between it and the last separator to your results, then look for the next separator.

string input = "1*1*5+89-43&33";
var separators = new[] { '+', '-', '*', '/', '&' };
var result = new List<string>();

int index;
int lastIndex = 0;
while ((index = input.IndexOfAny(separators, lastIndex)) != -1)
{
    // Add the text before the separator, if there is any
    if (index - lastIndex > 0)
    {
        result.Add(input.Substring(lastIndex, index - lastIndex));
    }
    
    // Add the separator itself
    result.Add(input[index].ToString());

    lastIndex = index + 1;  
}

// Add any text after the last separator
if (lastIndex < input.Length)
{
    result.Add(input.Substring(lastIndex));
}

Here's a basic and naive implementation that I beliewe will do what you want:

public static List<string> SplitExpression(string expression)
{
    var parts = new List<string>();
    
    bool isNumber(char c) => c == '.' || (c >= '0' && c <= '9');
    bool isOperator(char c) => !isNumber(c);

    int index = 0;
    while (index < expression.Length)
    {
        char c = expression[index];
        index++;

        if (isNumber(c))
        {
            int numberIndex = index - 1;
            while (index < expression.Length && isNumber(expression[index]))
                index++;
            parts.Add(expression.Substring(numberIndex, index - numberIndex));
        }
        else
            parts.Add(c.ToString());
    }

    // move unary signs into following number
    index = 0;
    while (index < parts.Count - 1)
    {
        bool isSign = parts[index] == "-" || parts[index] == "+";
        bool isFirstOrFollowingOperator = index == 0 || isOperator(parts[index - 1][0]);
        bool isPriorToNumber = isNumber(parts[index + 1][0]);
        
        if (isSign && isFirstOrFollowingOperator && isPriorToNumber)
        {
            parts[index + 1] = parts[index] + parts[index + 1];
            parts.RemoveAt(index);
        }
        else
            index++;
    }
    return parts;
}

Example input: "-1+-2*-10.1*.1" , and output:

-1 
+ 
-2 
* 
-10.1 
* 
.1 

Try with the following code snippet:

        string text  = "1*1*5+89-43&33";
        List<string> textsplit = new List<string>();
        foreach(var match in Regex.Matches(text, @"([*+/\-)(])|([0-9]+)"))
        {
            textsplit.Add(match.ToString());
        }

Result added as an image.

结果会是这样

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM