Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1749 How can I assign five substrings of a larger string to the variables named str1, str2... to str5?

You are here: irt.org | FAQ | JavaScript | Text | Q1749 [ previous next ]

You can use the eval() function to evaluate expressions at run time:

<html>

<head>

<script language="JavaScript"><!--
var str1, str2, str3, str4, str5;

function myFunction() {
  var somestring = 'abc/def/ghi/jkl/mno';

  var splitArray = somestring.split('/');

  for (var i=0; i<splitArray.length; i++) {
    eval('str' + (i+1) + '="' + splitArray[i] + '"');
  }
}

myFunction();

alert(str1 + '\n' + str2 + '\n' + str3 + '\n' + str4 + '\n' + str5);
//--></script>

</head>

<body>
...
</body>

</html>

©2018 Martin Webb