我如何分割字符串,在特定字符处断开?

我有这串

'john smith~123 Street~Apt 4~New York~NY~12345'

使用JavaScript,将其解析为最快的方法是

var name = "john smith";
var street= "123 Street";
//etc...

#1楼

即使这不是最简单的方法,也可以执行以下操作:

var addressString = "~john smith~123 Street~Apt 4~New York~NY~12345~",
    keys = "name address1 address2 city state zipcode".split(" "),
    address = {};

// clean up the string with the first replace
// "abuse" the second replace to map the keys to the matches
addressString.replace(/^~|~$/g).replace(/[^~]+/g, function(match){
    address[ keys.unshift() ] = match;
});

// address will contain the mapped result
address = {
    address1: "123 Street"
    address2: "Apt 4"
    city: "New York"
    name: "john smith"
    state: "NY"
    zipcode: "12345"
}

使用解构的ES2015更新

const [address1, address2, city, name, state, zipcode] = addressString.match(/[^~]+/g);

// The variables defined above now contain the appropriate information:

console.log(address1, address2, city, name, state, zipcode);
// -> john smith 123 Street Apt 4 New York NY 12345

#2楼

如果找到分离器,则仅

拆分

否则返回相同的字符串

 function SplitTheString(ResultStr) { if (ResultStr != null) { var SplitChars = '~'; if (ResultStr.indexOf(SplitChars) >= 0) { var DtlStr = ResultStr.split(SplitChars); var name = DtlStr[0]; var street = DtlStr[1]; } } } 

#3楼

Zach拥有这一权利。.使用他的方法,您还可以制作一个看似“多维”的数组。.我在JSFiddle http://jsfiddle.net/LcnvJ/2/上创建了一个快速示例

// array[0][0] will produce brian
// array[0][1] will produce james

// array[1][0] will produce kevin
// array[1][1] will produce haley

var array = [];
    array[0] = "brian,james,doug".split(",");
    array[1] = "kevin,haley,steph".split(",");

#4楼

您可以使用split分割文本。

另外,您也可以按照以下方式使用match

 var str = 'john smith~123 Street~Apt 4~New York~NY~12345'; matches = str.match(/[^~]+/g); console.log(matches); document.write(matches); 

正则表达式[^~]+将匹配~以外的所有字符,并以数组形式返回匹配项。 然后,您可以从中提取匹配项。


#5楼

根据ECMAScript6 ES6 ,干净的方法是破坏数组:

 const input = 'john smith~123 Street~Apt 4~New York~NY~12345'; const [name, street, unit, city, state, zip] = input.split('~'); console.log(name); // john smith console.log(street); // 123 Street console.log(unit); // Apt 4 console.log(city); // New York console.log(state); // NY console.log(zip); // 12345 

您可能在输入字符串中包含其他项。 在这种情况下,您可以使用rest运算符获取其余的数组,或者忽略它们:

 const input = 'john smith~123 Street~Apt 4~New York~NY~12345'; const [name, street, ...others] = input.split('~'); console.log(name); // john smith console.log(street); // 123 Street console.log(others); // ["Apt 4", "New York", "NY", "12345"] 

我应该以只读的方式引用值,并使用const声明。

享受ES6!


#6楼

使用此代码-

function myFunction() {
var str = "How are you doing today?";
var res = str.split("/");

}

#7楼

这个string.split("~")[0]; 把事情做好。

来源: String.prototype.split()


另一种使用咖喱和功能成分的功能方法。

因此,第一件事就是拆分功能。 我们要将这个"john smith~123 Street~Apt 4~New York~NY~12345"成这个["john smith", "123 Street", "Apt 4", "New York", "NY", "12345"]

const split = (separator) => (text) => text.split(separator);
const splitByTilde = split('~');

因此,现在我们可以使用我们专用的splitByTilde函数。 例:

splitByTilde("john smith~123 Street~Apt 4~New York~NY~12345") // ["john smith", "123 Street", "Apt 4", "New York", "NY", "12345"]

要获得第一个元素,我们可以使用list[0]运算符。 让我们构建first功能:

const first = (list) => list[0];

该算法是:用冒号分隔,然后获取给定列表的第一个元素。 因此,我们可以组合这些函数来构建最终的getName函数。 用reduce构建一个compose函数:

const compose = (...fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value);

现在使用它来组成splitByTildefirst函数。

const getName = compose(first, splitByTilde);

let string = 'john smith~123 Street~Apt 4~New York~NY~12345';
getName(string); // "john smith"

#8楼

由于逗号分割问题与该问题重复,因此请在此处添加。

如果要拆分一个字符并处理该字符后可能出现的多余空格(通常在逗号中出现),则可以使用replace然后split ,如下所示:

var items = string.replace(/,\s+/, ",").split(',')

#9楼

尝试使用纯Javascript

 //basic url=http://localhost:58227/ExternalApproval.html?Status=1

 var ar= [url,statu] = window.location.href.split("=");

#10楼

JavaScript:将字符串转换为数组JavaScript拆分

  var str = "This-javascript-tutorial-string-split-method-examples-tutsmake." var result = str.split('-'); console.log(result); document.getElementById("show").innerHTML = result; 
 <html> <head> <title>How do you split a string, breaking at a particular character in javascript?</title> </head> <body> <p id="show"></p> </body> </html> 

https://www.tutsmake.com/javascript-convert-string-to-array-javascript/


#11楼

使用JavaScript的String.prototype.split函数:

var input = 'john smith~123 Street~Apt 4~New York~NY~12345';

var fields = input.split('~');

var name = fields[0];
var street = fields[1];
// etc.

#12楼

您将要研究JavaScript的substrsplit ,因为这并不是真正适合jQuery的任务。


#13楼

您不需要jQuery。

var s = 'john smith~123 Street~Apt 4~New York~NY~12345';
var fields = s.split(/~/);
var name = fields[0];
var street = fields[1];

#14楼

好吧,最简单的方法是:

var address = theEncodedString.split(/~/)
var name = address[0], street = address[1]

#15楼

就像是:

var divided = str.split("/~/");
var name=divided[0];
var street = divided[1];

可能是最简单的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值