Connecting Tech Pros Worldwide Forums | Help | Site Map

Substring()

Inbaraj's Avatar
Member
 
Join Date: Apr 2007
Location: Puducherry <==> Coimbatore
Posts: 81
#1: Apr 12 '07
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
#2: Apr 13 '07

re: Substring()


Hi pal
maybe this will help you
or did u wanted to create an array of words?

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <!-- Created on: 10-4-2007 -->
  3. <head>
  4.   <title></title>
  5. <script language="JavaScript1.2"> 
  6. //the string
  7. var mystring = 'Is this what u ment? peace to u :)';//make sure there is no space at beginning of the string
  8. //set a seperator or leave empty
  9. var Seperator='<br>'; 
  10. function test1(){
  11. //if the string is emmpty do nothing
  12. if (mystring=='' || mystring==' '){/*alert ('mystring is empty');*/}
  13. else { 
  14. //get the first space
  15. getspace=mystring.indexOf(' ')+1;
  16. //if there is no space left , we assume it is the last word
  17. if (getspace==0){
  18. //get the remaining string length
  19. xxx=mystring.length; 
  20. //read the string from first char to its last
  21. firstword=mystring.substring(0,xxx);
  22. //read from position xxx to it remaining length *** seems useless but its needed
  23. newstring=mystring.substring(xxx,mystring.length);
  24. //write the firstword to span 
  25. InfoSpan.innerHTML+=' '+firstword;
  26. //make sure mystring is empty
  27. mystring='';
  28. }else{    
  29. //if there is a space left ,read the string from the first char to the first space
  30. firstword=mystring.substring(0,getspace); 
  31. //then write it to span
  32. InfoSpan.innerHTML+=' '+firstword+Seperator;
  33. //read the string from the first space to the end and assign it to newstring
  34. newstring=mystring.substring(getspace,mystring.length);
  35. //set the remaining string as mystring
  36. mystring=newstring;    
  37. //call function test2
  38. test2();
  39. }
  40. //test2 is the same as test1 :) 
  41. function test2(){  
  42. if (mystring=='' || mystring==' '){/*alert ('mystring is empty');*/}
  43. else {
  44. getspace1=mystring.indexOf(' ')+1;
  45. if (getspace1==0){
  46. xxx=mystring.length;
  47. Secondword=mystring.substring(0,xxx);
  48. newstring=mystring.substring(xxx,mystring.length); 
  49. InfoSpan.innerHTML+=' '+Secondword;
  50. mystring='';
  51. }else{    
  52. Secondword=mystring.substring(0,getspace1);    
  53. InfoSpan.innerHTML+=' '+Secondword+Seperator;
  54. newstring=mystring.substring(getspace1,mystring.length);
  55. mystring=newstring ;
  56. test1();
  57. }
  58. }
  59. </script>
  60. </head>
  61. <body>
  62.    <span id="InfoSpan" ></span><br><br> 
  63.  
  64.   <a onclick="test1()" style="cursor:pointer;text-decoration:underline;">try me</a> 
  65. </body>
  66. </html>
  67.  
peace
Member
 
Join Date: Mar 2007
Location: The Netherlands
Posts: 37
#3: Apr 13 '07

re: Substring()


Hi pal
maybe this will help you
or did u wanted to create an array of words?

Just ignore my previuos post :)
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <!-- Created on: 10-4-2007 -->
  3. <head>
  4.   <title></title>
  5. <script language="JavaScript1.2"> 
  6. //the string
  7. var mystring = 'Is this what u ment? peace to u :)';//make sure there is no space at beginning of the string
  8. //set a seperator or leave empty
  9. var Seperator='<br>'; 
  10. function test1(){
  11. //if the string is emmpty do nothing
  12. if (mystring=='' || mystring==' '){/*alert ('mystring is empty');*/}
  13. else { 
  14. //get the first space
  15. getspace=mystring.indexOf(' ')+1;
  16. //if there is no space left , we assume it is the last word
  17. if (getspace==0){
  18. //get the remaining string length
  19. xxx=mystring.length; 
  20. //read the string from first char to its last
  21. firstword=mystring.substring(0,xxx);
  22. //read from position xxx to it remaining length *** seems useless but its needed
  23. newstring=mystring.substring(xxx,mystring.length);
  24. //write the firstword to span 
  25. InfoSpan.innerHTML+=' '+firstword;
  26. //make sure mystring is empty
  27. mystring='';
  28. }else{    
  29. //if there is a space left ,read the string from the first char to the first space
  30. firstword=mystring.substring(0,getspace); 
  31. //then write it to span
  32. InfoSpan.innerHTML+=' '+firstword+Seperator;
  33. //read the string from the first space to the end and assign it to newstring
  34. newstring=mystring.substring(getspace,mystring.length);
  35. //set the remaining string as mystring
  36. mystring=newstring;    
  37. //call function test2
  38. test1();
  39. }
  40.  
  41. </script>
  42. </head>
  43. <body>
  44.    <span id="InfoSpan" ></span><br><br> 
  45.  
  46.   <a onclick="test1()" style="cursor:pointer;text-decoration:underline;">try me</a> 
  47. </body>
  48. </html>
  49.  
peace
Reply