简体   繁体   中英

How to split a string based on specific character position

I have the following string as an input, and I need to break (parse) it into pieces based on specific known positions, ie:

string = 374450012349999106006104034174003704175003704 position 0,2 - 37 position 3,5 - 445 position 6,10 - 123 etc.

how do I do this using javascript, and then display the results in html? sorry if silly

Thanks!

The method .substring(start, stop) is generally used for this type of activity. Where start is the index in the string which you wish to begin the substring and stop indicates the index position you wish to end on but not include .

For example,

To display in HTML one could simply:

<h1 id="myDisplay></h1>

JavaScript

var display = document.getElementById("myDisplay");
var myString = "374450012349999106006104034174003704175003704";
var pos1 = myString.substring(0,2);
// Repeat for Pos2 and 3;

display.innerHTML = pos 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM