how to parse this string with java script
19 51 2.108997
20 47 2.1089
like this
<span>19 51</span> <span>2.108997</span>
<span>20 47</span> <span>2.1089</span>
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
You can split a string by each character using an empty string('') as the splitter. In the example below, we split the same message using an empty string. The result of the split will be an array containing all the characters in the message string.
The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
The split() is an inbuilt function in TypeScript which is used to splits a String object into an array of strings by separating the string into sub-strings. Parameter: This method accepts two parameter as mentioned above and described below: separator – This parameter is the character to use for separating the string.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With