简体   繁体   English

如何用多个字符分割字符串变量

[英]How to split string variable by multiple characters

I need to split string variable by multiple characters, in my case by " " ; 我需要将字符串变量拆分为多个字符,在我的情况下,应使用" " ; Here is my code: 这是我的代码:

string s_var = "New   String   variable";
string[] s_mas = s_var.Split("   ");

The Split() method isn't working for me, it says that the argument " " is invalid. Split()方法不适用于我,它说参数" "无效。 Hoping you guys know how to solve this issue. 希望你们知道如何解决这个问题。

You're not specifying the correct arguments. 您没有指定正确的参数。

  • If you want to split by a string, you need to specify an array. 如果要按字符串分割,则需要指定一个数组。
  • You also need to specify whether or not to discard empty strings. 您还需要指定是否丢弃空字符串。

Try this: 尝试这个:

var s_mas = s_var.Split(new[] { "   " }, StringSplitOptions.None);

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

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