I'm trying to cut a string in different part, and return each part into a different input. Here's an example:
Consider the following string: Hello|World|!
As you can see, there is a separator, which is |
. I'd like to separate each part of the string cut by this |
and return them into a different input.
I can't use str.substr()
(at least the usual use of it, maybe you have a trick) because the number of characters between the |
is not fixed, sometimes it will be 2 characters, sometimes 16, and from what I know str.substr()
requires to set the number of characters before the cut.
Basically I'm looking for a solution as Excel provides in the Convert feature, where you define a separator and Excel will separate each part of the value into different cells each times he finds the separator set.
This is the first part of the Javascript, taking the value of the textarea where the string is pasted:
function cutTheString()
{
var str = document.getElementById('textarea').value;
}
And this would be the HTML:
<textarea id="textarea"></textarea>
<br />
<input type="submit" value="Cut string" onClick="cutTheString()" />
<input type="text" id="string1"></input>
<input type="text" id="string2"></input>
<input type="text" id="string3"></input>
For now I have document.getElementById('string1').value = str();
which returns the full string into the first input. I also have the possibility to transform the separator |
into ,
by using var res = str.split("|");
but that doesn't really help my case.