How can I split and trim a string into parts all on one line?

前端 未结 8 1157
一个人的身影
一个人的身影 2020-12-22 21:08

I want to split this line:

string line = \"First Name ; string ; firstName\";

into an array of their trimmed versions:

\"Fi         


        
8条回答
  •  误落风尘
    2020-12-22 21:20

    Use Regex

    string a="bob, jon,man; francis;luke; lee bob";
    			String pattern = @"[,;\s]";
                String[] elements = Regex.Split(a, pattern).Where(item=>!String.IsNullOrEmpty(item)).Select(item=>item.Trim()).ToArray();;			
                foreach (string item in elements){
                    Console.WriteLine(item.Trim());

    Result:

    bob

    jon

    man

    francis

    luke

    lee

    bob

    Explain pattern [,;\s]: Match one occurrence of either the , ; or space character

提交回复
热议问题