简体   繁体   English

如何通过Node Js分割字符串?

[英]How to split a string by Node Js?

My array: 我的数组:

var str=['data1,data2 '];

I have used: 我用过:

var arr = str.split(",");

But one error is showed. 但是显示了一个错误。 TypeError: Object data1,data2 has no method 'split'. TypeError:对象data1,data2没有方法'split'。 How can I solve this problem. 我怎么解决这个问题。

My output will be: 我的输出将是:

arr= data1,data2
// or
arr[0]=data1;
arr[1]=data2;

How can I solve this problem ? 我怎么解决这个问题 ?

You should do this : 你应该做这个 :

var arr = str.toString().split(",");

" TypeError: Object data1,data2 has no method 'split' " indicates the variable is not considered as a string. TypeError:对象data1,data2没有方法'split' ”指示该变量不被视为字符串。 Therefore, you must typecast it. 因此,您必须进行强制转换。


update 08.10.2015 I have noticed someone think the above answer is a " dirty workaround " and surprisingly this comment is upvoted. 更新2015年10月10日我注意到有人认为以上答案是“ 肮脏的解决方法 ”,令人惊讶的是,此评论遭到了批评。 In fact it is the exact opposite - using str[0].split(",") as 3 (!) other suggests is the real "dirty workaround". 实际上,这是完全相反的-使用str[0].split(",")作为3(!),其他建议是真正的“肮脏解决方法”。 Why? 为什么? Consider what would happen in these cases : 考虑在这些情况下会发生什么:

var str = [];
var str = ['data1,data2','data3,data4'];
var str = [someVariable]; 

str[0].split(",") will fail utterly as soon str holds an empty array, for some reason not is holding a String.prototype or will give an unsatisfactory result if str holds more than one string. 一旦str拥有一个空数组, str[0].split(",")就会彻底失败,由于某种原因而不是拥有String.prototype或者如果str拥有多个字符串,则结果将不令人满意。 Using str[0].split(",") blindly trusting that str always will hold 1 string exactly and never something else is bad practice. 使用str[0].split(",")盲目地相信str总是将精确地容纳1个字符串,并且绝不会其他任何东西是不好的做法。 toString() is supported by numbers, arrays, objects, booleans, dates and even functions; 数字,数组,对象,布尔值,日期甚至函数都支持toString() str[0].split() has a huge potential of raising errors and stop further execution in the scope, and by that crashing the entire application. str[0].split()具有引发错误并停止作用域进一步执行的巨大潜力,从而使整个应用程序崩溃。

If you really, really want to use str[0].split() then at least do some minimal type checking : 如果您确实要使用str[0].split()那么至少要进行一些最小的类型检查:

var arr;
if (typeof str[0] == 'string') {
    arr = str[0].split(',')
} else {
    arr = [];
}

If your starting point is a array with a string inside. 如果您的起点是一个内部带有stringarray This should work: 这应该工作:

var arr = str[0].split(",");

Otherwise you should have a string as starting point for your code to work as you expected: 否则,您应该以字符串作为起点,代码才能按预期工作:

var str = 'data1,data2';

If you have more elements in the array you will need to iterate them with a for loop. 如果数组中有更多元素,则需要使用for循环对其进行迭代。

Edit to add other cases: 编辑以添加其他情况:

If you have several strings in that array, then you should be more carefull and do something like this: 如果该数组中有多个字符串,则应格外小心,并执行以下操作:

 var str = ['data1,data2 ', ' data3, data4 ']; // notice these strings have many spaces in different places var longString = str.join(','); var array = longString.split(',').map(s => s.trim()).filter(Boolean); // removing spaces from start and end of strings, and eventually removing empty positions console.log(array); 

As you said, str is an array (with one element). 如您所说, str是一个数组(带有一个元素)。 If you want to split the string contained in the array, you have to access the array first: 如果要拆分数组中包含的字符串,则必须首先访问该数组:

var arr = str[0].split(",");

let's say we have two date : 假设我们有两个日期:

date1: 17/01/1989 date1:​​1989年1月17日

date2: 20/02/2000 date2:20/02/2000

if we want to compare them just split the string and compare like this 如果我们想比较它们,只需将字符串分开并像这样比较

var date1= date1.toString().split("/");

var date2= date2.toString().split("/");

var a = parseInt(date1[2] + date1[1] + date1[0]);

var b = parseInt(date2[2] + date2[1] + date2[0]);

if(a < b){ 如果(a <b){

alert("date2 bigger than date1")} alert(“ date2比date1大”)}

} else if(a > b){ }否则if(a> b){

alert("date1 bigger than date2") 警报(“ date1大于date2”)

} else{ }其他{

alert("date 1 and date2 are equals "); alert(“ date 1和date2等于”);

} }

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

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