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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:43:20  来源:igfitidea点击:

how to split String based on \r\n

javascriptjqueryarrayssplit

提问by Pavan Alapati

We are Developing phonegapapplication.We get Data form CSVfile. It's look like this We getting data like this

我们正在开发phonegap应用程序CSV。我们得到数据表单文件。它看起来像这样我们得到这样的数据

We need Data Split into two strings Like
String1 enter image description here

我们需要将数据拆分为两个字符串 像
String1在此处输入图片说明

String2 enter image description here

字符串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

Fiddle

小提琴

回答by Victor Lin

try this:

试试这个:

var split = string.split(/\n/);