admin管理员组

文章数量:1309939

Im trying to split a sting variable into an array with each character in its own position in the array, but cant seem to get this to work

 function test() {
    var strString = "thisIsTheString";
    var test = @Explode(strString, "");
    var strReturn = "";

    for (var i = 0; i < @Length(test); i++) {
        strReturn += test[i] + "<br/>";
    }

    return strReturn;
}

Im trying to split a sting variable into an array with each character in its own position in the array, but cant seem to get this to work

 function test() {
    var strString = "thisIsTheString";
    var test = @Explode(strString, "");
    var strReturn = "";

    for (var i = 0; i < @Length(test); i++) {
        strReturn += test[i] + "<br/>";
    }

    return strReturn;
}
Share Improve this question edited Jul 10, 2013 at 13:39 Naveen 6,93610 gold badges41 silver badges85 bronze badges asked Jul 10, 2013 at 12:11 Hosea KambondeHosea Kambonde 3011 gold badge4 silver badges13 bronze badges 2
  • Maybe you can use split instead of explode? – Pieter Commented Jul 10, 2013 at 12:25
  • 2 You have tagged this question Lotusscript but your code snippet is not Lotusscript but rather server-side JavaScript :-) – Per Henrik Lausten Commented Jul 10, 2013 at 12:39
Add a ment  | 

2 Answers 2

Reset to default 8

The simplest way would be to use split function by passing empty string to it. For e.g.

var str = "this is a string";
var arr = str.split("");

@Explode uses as delimiters space, ma and semicolon if second parameter is empty. That doesn't help you in your case. Just use "normal" string functions like substring():

function test() {
    var strString = "thisIsTheString";
    var strReturn = "";

    for (var i = 0; i < strString.length; i++) {
        strReturn += strString.substring(i, i+1) + "<br/>";
    }

    return strReturn;
}

In case you really need an array of characters then code would look like this:

var strString = "thisIsTheString";
var arrayReturn = new Array();
for (var i = 0; i < strString.length; i++) {
    arrayReturn[i] = strString.substring(i, i+1);
}

本文标签: javascriptSplit all characters in a string with no delimiter into an array with lotus notesStack Overflow