How do I split a string, breaking at a particular character?

后端 未结 17 2387
误落风尘
误落风尘 2020-11-21 05:07

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



        
相关标签:
17条回答
  • 2020-11-21 05:33

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

    0 讨论(0)
  • 2020-11-21 05:33

    well, easiest way would be something like:

    var address = theEncodedString.split(/~/)
    var name = address[0], street = address[1]
    
    0 讨论(0)
  • 2020-11-21 05:34

    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/

    // 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(",");
    
    0 讨论(0)
  • 2020-11-21 05:38

    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

    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
    
    0 讨论(0)
  • 2020-11-21 05:38

    This isn't as good as the destructuring answer, but seeing as this question was asked 12 years ago, I decided to give it an answer that also would have worked 12 years ago.

    function Record(s) {
        var keys = ["name", "address", "address2", "city", "state", "zip"], values = s.split("~"), i
        for (i = 0; i<keys.length; i++) {
            this[keys[i]] = values[i]
        }
    }
    
    var record = new Record('john smith~123 Street~Apt 4~New York~NY~12345')
    
    record.name // contains john smith
    record.address // contains 123 Street
    record.address2 // contains Apt 4
    record.city // contains New York
    record.state // contains NY
    record.zip // contains zip
    
    0 讨论(0)
  • 2020-11-21 05:39

    JavaScript: Convert String to Array JavaScript Split

        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/

    0 讨论(0)
提交回复
热议问题