我如何分割一个字符串,破坏一个特定的字符?

时间:2021-10-27 02:29:34

I have this string

我有这个字符串

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

Using JavaScript, what is the fastest way to parse this into

使用JavaScript,最快的解析方法是什么

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

11 个解决方案

#1


702  

With JavaScript’s String.prototype.split function:

JavaScript的String.prototype。拆分功能:

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.

#2


44  

You don't need jQuery.

你不需要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];

#3


15  

Even though this is not the simplest way, you could do this:

尽管这不是最简单的方法,但你可以这样做:

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"
}

Update for ES2015, using destructuring

更新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

#4


14  

According to ECMAScript6 ES6, the clean way is destructing arrays:

根据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

You may have extra items in the input string. In this case, you can use rest operator to get an array for the rest or just ignore them:

您可能在输入字符串中有额外的项。在这种情况下,可以使用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"]

I supposed a read-only reference for values and used the const declaration.

我假设值为只读引用,并使用const声明。

Enjoy ES6!

享受ES6 !

#5


12  

You'll want to look into JavaScript's substr or split as this is not really a task suited for jQuery

您需要查看JavaScript的substr或split,因为这并不是真正适合jQuery的任务。

#6


5  

well, easiest way would be something like:

最简单的方法是

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

#7


5  

If Spliter is found then only

如果是拆分器,则只找到

Split it

把它

else return the same string

否则返回相同的字符串

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];
        }
    }
}

#8


4  

Something like:

喜欢的东西:

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

Is probably going to be easiest

可能最简单?

#9


3  

You can use split to split the text.

可以使用split来拆分文本。

As an alternative, you can also use match as follow

作为一种选择,您也可以使用match作为follow。

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

console.log(matches);
document.write(matches);

The regex [^~]+ will match all the characters except ~ and return the matches in an array. You can then extract the matches from it.

regex ^ ~ +将匹配所有的人物除了~并返回匹配数组中。然后可以从中提取匹配项。

#10


2  

Zach had this one right.. using his method you could also make a seemingly "multi-dimensional" array.. I created a quick example at JSFiddle http://jsfiddle.net/LcnvJ/2/

扎克说得对。使用他的方法,你也可以制作一个看似“多维”的数组。我在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(",");

#11


0  

Use this code------

使用这个代码- - - - - -

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

}

#1


702  

With JavaScript’s String.prototype.split function:

JavaScript的String.prototype。拆分功能:

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.

#2


44  

You don't need jQuery.

你不需要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];

#3


15  

Even though this is not the simplest way, you could do this:

尽管这不是最简单的方法,但你可以这样做:

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"
}

Update for ES2015, using destructuring

更新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

#4


14  

According to ECMAScript6 ES6, the clean way is destructing arrays:

根据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

You may have extra items in the input string. In this case, you can use rest operator to get an array for the rest or just ignore them:

您可能在输入字符串中有额外的项。在这种情况下,可以使用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"]

I supposed a read-only reference for values and used the const declaration.

我假设值为只读引用,并使用const声明。

Enjoy ES6!

享受ES6 !

#5


12  

You'll want to look into JavaScript's substr or split as this is not really a task suited for jQuery

您需要查看JavaScript的substr或split,因为这并不是真正适合jQuery的任务。

#6


5  

well, easiest way would be something like:

最简单的方法是

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

#7


5  

If Spliter is found then only

如果是拆分器,则只找到

Split it

把它

else return the same string

否则返回相同的字符串

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];
        }
    }
}

#8


4  

Something like:

喜欢的东西:

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

Is probably going to be easiest

可能最简单?

#9


3  

You can use split to split the text.

可以使用split来拆分文本。

As an alternative, you can also use match as follow

作为一种选择,您也可以使用match作为follow。

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

console.log(matches);
document.write(matches);

The regex [^~]+ will match all the characters except ~ and return the matches in an array. You can then extract the matches from it.

regex ^ ~ +将匹配所有的人物除了~并返回匹配数组中。然后可以从中提取匹配项。

#10


2  

Zach had this one right.. using his method you could also make a seemingly "multi-dimensional" array.. I created a quick example at JSFiddle http://jsfiddle.net/LcnvJ/2/

扎克说得对。使用他的方法,你也可以制作一个看似“多维”的数组。我在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(",");

#11


0  

Use this code------

使用这个代码- - - - - -

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

}