javascript 如何根据 \r\n 拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29558519/
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
how to split String based on \r\n
提问by Pavan Alapati
We are Developing phonegap
application.We get Data form CSV
file. It's look like this
我们正在开发phonegap
应用程序CSV
。我们得到数据表单文件。它看起来像这样
We need Data Split into two strings Like
String1
我们需要将数据拆分为两个字符串 像
String1
String2
字符串2
We tried like this but We don't have luck so Please guide me
我们像这样尝试过但我们没有运气所以请指导我
var split = string.split('\r\n');
Please help me
请帮我
采纳答案by Brijesh Bhatt
Replace the new line characters with space and then split the string with space
用空格替换换行符,然后用空格分割字符串
string.replace( /\n/g, " " ).split(" ");
UPDATE:
更新:
var string1=string.substring(0,string.indexOf("TOTALAMOUNT"));
var string2=string.substring(string.indexOf("TOTALAMOUNT"),string.length);
Or if your string contains \n then:
或者,如果您的字符串包含 \n,则:
var string1=string.substring(0,string.indexOf("\n"));
var string2=string.substring(string.indexOf("\n"),string.length);
alert(string1);
alert(string
回答by Victor Lin
try this:
试试这个:
var split = string.split(/\n/);