Javascript 根据分隔符拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5648075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
splitting a string based on delimiter
提问by jslearner
Possible Duplicate:
How do I split a string, breaking at a particular character?
可能的重复:
如何拆分字符串,在特定字符处断开?
I have a string in following format
我有以下格式的字符串
part1/part2
/ is the delimiter
now I want to get split the string and get part 1. How can I do it?
现在我想拆分字符串并获取第 1 部分。我该怎么做?
回答by x10
result = "part1/part2".split('/')
result[0] = "part1"
result[1] = "part2
回答by KooiInc
split the string and get part 1
拆分字符串并获得第 1 部分
'part1/part2'.split('/')[0]
回答by alex
var tokens = 'part1/part2'.split('/');
回答by Teneff
var delimeter = '/';
var string = 'part1/part2';
var splitted = string.split(delimeter);
alert(splitted[0]); //alert the part1
回答by c3p0
var result = YourString.split('/');
For your example result will be an array with 2 entries: "part1" and "part2"
对于您的示例,结果将是一个包含 2 个条目的数组:“part1”和“part2”