Substring()  | Member | | Join Date: Apr 2007 Location: Puducherry <==> Coimbatore
Posts: 81
| | |
Hi..
I have given a string input as h="Hello:world". I want to divide the sitring as "Hello" and "world".. I have divided the string with ":" Plz help me how to do this....
Input as
~~~~~~
h="Hello:world";
Output as
~~~~~~~
h1="Hello"
h2="world"
I need the javascript code for this... Help me
Thanks in advance...
Reg
Inba
| | Member | | Join Date: Mar 2007 Location: The Netherlands
Posts: 37
| | | re: Substring()
Hi pal
maybe this will help you
or did u wanted to create an array of words? -
<html>
-
<!-- Created on: 10-4-2007 -->
-
<head>
-
<title></title>
-
<script language="JavaScript1.2">
-
//the string
-
var mystring = 'Is this what u ment? peace to u :)';//make sure there is no space at beginning of the string
-
//set a seperator or leave empty
-
var Seperator='<br>';
-
function test1(){
-
//if the string is emmpty do nothing
-
if (mystring=='' || mystring==' '){/*alert ('mystring is empty');*/}
-
else {
-
//get the first space
-
getspace=mystring.indexOf(' ')+1;
-
//if there is no space left , we assume it is the last word
-
if (getspace==0){
-
//get the remaining string length
-
xxx=mystring.length;
-
//read the string from first char to its last
-
firstword=mystring.substring(0,xxx);
-
//read from position xxx to it remaining length *** seems useless but its needed
-
newstring=mystring.substring(xxx,mystring.length);
-
//write the firstword to span
-
InfoSpan.innerHTML+=' '+firstword;
-
//make sure mystring is empty
-
mystring='';
-
}else{
-
//if there is a space left ,read the string from the first char to the first space
-
firstword=mystring.substring(0,getspace);
-
//then write it to span
-
InfoSpan.innerHTML+=' '+firstword+Seperator;
-
//read the string from the first space to the end and assign it to newstring
-
newstring=mystring.substring(getspace,mystring.length);
-
}
-
//set the remaining string as mystring
-
mystring=newstring;
-
//call function test2
-
test2();
-
}
-
}
-
//test2 is the same as test1 :)
-
function test2(){
-
if (mystring=='' || mystring==' '){/*alert ('mystring is empty');*/}
-
else {
-
getspace1=mystring.indexOf(' ')+1;
-
if (getspace1==0){
-
xxx=mystring.length;
-
Secondword=mystring.substring(0,xxx);
-
newstring=mystring.substring(xxx,mystring.length);
-
InfoSpan.innerHTML+=' '+Secondword;
-
mystring='';
-
}else{
-
Secondword=mystring.substring(0,getspace1);
-
InfoSpan.innerHTML+=' '+Secondword+Seperator;
-
newstring=mystring.substring(getspace1,mystring.length);
-
}
-
mystring=newstring ;
-
test1();
-
}
-
}
-
</script>
-
</head>
-
<body>
-
<span id="InfoSpan" ></span><br><br>
-
-
<a onclick="test1()" style="cursor:pointer;text-decoration:underline;">try me</a>
-
</body>
-
</html>
-
peace
| | Member | | Join Date: Mar 2007 Location: The Netherlands
Posts: 37
| | | re: Substring()
Hi pal
maybe this will help you
or did u wanted to create an array of words?
Just ignore my previuos post :) -
<html>
-
<!-- Created on: 10-4-2007 -->
-
<head>
-
<title></title>
-
<script language="JavaScript1.2">
-
//the string
-
var mystring = 'Is this what u ment? peace to u :)';//make sure there is no space at beginning of the string
-
//set a seperator or leave empty
-
var Seperator='<br>';
-
function test1(){
-
//if the string is emmpty do nothing
-
if (mystring=='' || mystring==' '){/*alert ('mystring is empty');*/}
-
else {
-
//get the first space
-
getspace=mystring.indexOf(' ')+1;
-
//if there is no space left , we assume it is the last word
-
if (getspace==0){
-
//get the remaining string length
-
xxx=mystring.length;
-
//read the string from first char to its last
-
firstword=mystring.substring(0,xxx);
-
//read from position xxx to it remaining length *** seems useless but its needed
-
newstring=mystring.substring(xxx,mystring.length);
-
//write the firstword to span
-
InfoSpan.innerHTML+=' '+firstword;
-
//make sure mystring is empty
-
mystring='';
-
}else{
-
//if there is a space left ,read the string from the first char to the first space
-
firstword=mystring.substring(0,getspace);
-
//then write it to span
-
InfoSpan.innerHTML+=' '+firstword+Seperator;
-
//read the string from the first space to the end and assign it to newstring
-
newstring=mystring.substring(getspace,mystring.length);
-
}
-
//set the remaining string as mystring
-
mystring=newstring;
-
//call function test2
-
test1();
-
}
-
}
-
-
</script>
-
</head>
-
<body>
-
<span id="InfoSpan" ></span><br><br>
-
-
<a onclick="test1()" style="cursor:pointer;text-decoration:underline;">try me</a>
-
</body>
-
</html>
-
peace
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|