首页 文章

如何分割字符串,打破特定字符?

提问于
浏览
487

我有这个字符串

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

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

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

13 回答

  • 2

    好吧,最简单的方法是这样的:

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

    如果只有 Spliter is found 那么

    拆分它

    否则返回 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];
    }
    }
    }

  • 5

    就像是:

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

    可能是最简单的

  • 12

    您可以使用 split 拆分文本 .

    作为替代方案,您也可以使用 match ,如下所示

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

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

  • 31

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

    根据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 声明 .

    Enjoy ES6!

  • 16

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

    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

    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
    
  • 5

    你'll want to look into JavaScript' s substrsplit因为这不是一个真正适合jQuery的任务

  • 0

    使用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.
    
  • 0

    扎克有这个权利..使用他的方法你也可以做一个看似"multi-dimensional"数组..我在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(",");
    
  • 775

    使用此代码------

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

    string.split("~")[0]; 完成了事情 .

    来源:String.prototype.split()


    Another functional approach using curry and function composition.

    所以第一件事就是拆分功能 . 我们想把这个 "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"
    
  • 48

    由于逗号分裂问题与此问题重复,请在此处添加 .

    如果你想分割一个角色并且还要处理可能跟随该角色的额外空格(通常用逗号进行),你可以使用 replace 然后 split ,如下所示:

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

相关问题