繁体   English   中英

如何将C#中的字符串划分为子字符串

[英]How to divide string in c# to substrings

我有以下方法

 public void DivideIntoSubStrings(string msg, string methodName, string  userId)
 {   
    string st = String.Format("{0}.{1}.{2}", msg, methodName, userId);
    DoSomething(st);
 }

当我在上面的方法中调用以下方法并想要获取msg,methodName和userId参数时

 public void DoSomething(string bigString)
 {
       string st1 = bigString;
       string[] st2 = st1.Split('.');
       string t1 = st2[st2.Length - 1];
       string t2 = st2[st2.Length - 2];
       Console.WriteLine(t1);
       Console.WriteLine(t2);

 }

我总是确保获得methodName和userId,因为它们已传递到DivideIntoSubString(....)方法中,但是msg是消息参数,可以在其中键入任何内容,或者换句话说,我不知道msg的格式预先输入字符串,因为它将在运行时传递。 它可以包含逗号,句点,下划线等。因此,在运行时传递的msg参数的格式相同时,最好获得完全相同的值。

一种棘手的方法是反转bigString字符串,使用最大元素数的重载来拆分字符串:

string st1 = new string(bigString.Reverse().ToArray());    
string[] st2 = st1.Split(new char[] { '.' }, 3);

string user = new string(st2[0].Reverse().ToArray());
string method = new string(st2[1].Reverse().ToArray());
string msg = new string(st2[2].Reverse().ToArray());

Console.WriteLine(user);
Console.WriteLine(method);
Console.WriteLine(msg);

请注意,您需要再次反转分割后的字符串以获取原始

您可以使用string.Joinmsg string.Join部分再次合并在一起:

public void DoSomething(string bigString)
{
   string st1 = bigString;
   string[] st2 = st1.Split('.');
   string t1 = st2[st2.Length - 1];
   string t2 = st2[st2.Length - 2];

   string msg = string.Join(".", st2.Take(st2.Length-2));    

   Console.WriteLine(t1);
   Console.WriteLine(t2);
   Console.WriteLine(msg);
}    

LINQ方法Take返回仅表示st2的第一个st2.Length-2元素的序列。
string.Join通过用句点再次定界将该序列中的所有字符串连接在一起。

请尝试以下代码:

public static void DivideIntoSubStrings(string msg, string methodName, string userId)
{
    string lengths = String.Format("{0}%{1}%{2}", msg.Length, methodName.Length, userId.Length);
    string st = String.Format("{0}.{1}.{2}.{3}", lengths, msg, methodName, userId);
    DoSomething(st);
}

public static void DoSomething(string bigString)
{ 
    string lengthsString = bigString.Split('.').ElementAt(0);
    List<string> lengthsStringArray = lengthsString.Split('%').ToList();
    List<int> lengthIntArray = new List<int>();
    for (int i=0; i < lengthsStringArray.Count; i++)
    {
        lengthIntArray.Add(int.Parse(lengthsStringArray.ElementAt(i))); 
    }
    string msg = bigString.Substring(lengthsString.Length + 1, lengthIntArray.ElementAt(0));
    string methodName = bigString.Substring(lengthsString.Length + msg.Length + 2, lengthIntArray.ElementAt(1));
    string userId = bigString.Substring(lengthsString.Length + msg.Length + methodName.Length + 3, lengthIntArray.ElementAt(2));

    Console.WriteLine(msg);
    Console.WriteLine(methodName);
    Console.WriteLine(userId);
}

您需要知道字符串长度,然后才能将其保留在传递的字符串中。 这样,您将永远不必担心您选择的加入字符!

我认为应该对此进行修改。

public void DivideIntoSubStrings(string msg, string methodName, string  userId)
{   
   string[] st = new string[3] { msg, methodName, userId };
   DoSomething(st);
}

public void DoSomething(string[] params)
{

}

或者,您可以将其作为Tuple<string, string, string>传递。 或者,如果您不想存储在一个变量中,则只需将msg,methodName和userId作为三个参数传递给您的方法。 没有理由将所有内容串联起来,只是立即将其再次拆分。

但是,如果您真的想这样做...

如果没有人可以传递新行(例如,源是单行文本框),我建议您使用{0}\\r\\n{1}\\r\\n{2}作为格式,而不是全局的格式作为一个.

那你可以用

bigString.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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