Split string with JavaScript

后端 未结 3 1731
萌比男神i
萌比男神i 2021-01-02 17:44

how to parse this string with java script

19 51 2.108997
20 47 2.1089

like this

19 51 2.108997         


        
相关标签:
3条回答
  • 2021-01-02 18:00

    Assuming you're using jQuery..

    var input = '19 51 2.108997\n20 47 2.1089';
    var lines = input.split('\n');
    var output = '';
    $.each(lines, function(key, line) {
        var parts = line.split(' ');
        output += '<span>' + parts[0] + ' ' + parts[1] + '</span><span>' + parts[2] + '</span>\n';
    });
    $(output).appendTo('body');
    
    0 讨论(0)
  • 2021-01-02 18:14

    Like this:

    var myString = "19 51 2.108997";
    var stringParts = myString.split(" ");
    var html = "<span>" + stringParts[0] + " " + stringParts[1] + "</span> <span>" + stringParts[2] + "</span";
    
    0 讨论(0)
  • 2021-01-02 18:14
    var wrapper = $(document.body);
    
    strings = [
        "19 51 2.108997",
        "20 47 2.1089"
    ];
    
    $.each(strings, function(key, value) {
        var tmp = value.split(" ");
        $.each([
            tmp[0] + " " + tmp[1],
            tmp[2]
        ], function(key, value) {
            $("<span>" + value + "</span>").appendTo(wrapper);
        }); 
    });
    
    0 讨论(0)
提交回复
热议问题