字符串操作" ^"以字符串形式显示消息的结尾

时间:2013-04-02 06:31:55

标签: c# .net

C#代码......

     string[] array = new string[] { "12", "34", "5^67", "8^945", "^63^", "9484^" };
                    foreach(string message in array)
                    {
                              FindString(message)

                    }
 string remainingString = "";
        string tempString = "";
private void FindString(string message)
        {

            if(message.Contains("^"))
            {//remove char ^ process
                int pos = -1;
                pos = message.IndexOf('^');
                outputString = message.Substring(0, pos);
                remainingString = message.Substring(pos);
                if(tempString != "")
                {
                    outputString = tempString + outputString;
                    tempString = "";
                }
            }
            else
            {
                tempString = tempString + message;
            }

        }

从数组我必须得到消息12345,678,945,63,9484。 “^”以字符串

显示消息的结尾

2 个答案:

答案 0 :(得分:1)

string[] array = new string[] { "12", "34", "5^67", "8^945", "^63^", "9484^" };
                foreach(string message in array)
                {
                          FindString(message)

                }
 string remainingString = "";
 string tempString = "";
 private void FindString(string message)
    {

        while(message.Contains("^"))
        {//remove char ^ process
            int pos = -1;
            pos = message.IndexOf('^');
            outputString = message.Substring(0, pos);
            message = message.Substring(pos + 1); // <--- LOOK HERE
            if(tempString != "")
            {
                outputString = tempString + outputString;
                tempString = "";
            }

        }

        tempString = tempString + message;


    }

当然,更好的选择是string.Join数组,然后是.Split结果:

string[] answers = string.Join("", messages).Split("^").ToArray();

答案 1 :(得分:0)

尝试按输入字符串拆分:

var output = string.split('^').ToList();