繁体   English   中英

在特殊字符上拆分字符串

[英]Splitting a string on special characters

我有一串未定义的元素,用~~分隔,例如:

foobar~~some example text~~this is a string

如何将这些值拆分为一个简单的数组,然后我可以循环?

我试图用逗号替换带有正则表达式的~~所以我可以将其作为csv来处理,但必须有一个更简单的方法吗?

使用拆分方法

var str = "foobar~~some example text~~this is a string";
var arr = str.split('~~');
$.each(arr,function(indx,val){
   console.log(val);
});

或者你可以像arr[0]arr[1]arr[2] ....

Ouptput

   "foobar"
   "some example text"
   "this is a string"

使用String.Split()

string str = "foobar~~some example text~~this is a string";
string[] _result = str.Split("~~", StringSplitOptions.None);

您将需要使用Split方法。 您可以执行以下操作:

string[] arr=yourString.Split("~~");
foreach(string arrElement in arr)
{
   //do what ever you want to
}

暂无
暂无

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

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