简体   繁体   中英

Why string can't be split at \r\n?

I have a string containing \r\n. I am unable to split it. Any help would be appreciated.

Code:

let xyz = String(JSON.stringify(data[property].Users__c));
            let arr = xyz.split("\r\n");
            console.log('XYZ :' + xyz);
            console.log('ARR: ' + arr);

SS of Edge: 在此处输入图像描述

When calling JSON.stringify on a string, newline characters are escaped and the string is wrapped in quotes. It looks like you want neither to happen. Note also that applying String to a string is useless.

So just drop those two function calls:

let xyz = data[property].Users__c;
let arr = xyz.split("\r\n");
console.log('XYZ :' + xyz);
console.log('ARR: ' + arr);

If for some reason the Users__c property would be some other data type than a string, then the question is what you expect split to produce... You could keep the String call to avoid errors in that case, but you'd need to look into what you then really want to happen:

let xyz = String(data[property].Users__c);
let arr = xyz.split("\r\n");
console.log('XYZ :' + xyz);
console.log('ARR: ' + arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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