473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Substring()

Inbaraj
76
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
Apr 12 '07 #1
2 1746
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
Apr 12 '07 #2
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
Apr 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Radhika Sambamurti | last post by:
Hi, I've written a substring function. The prototype is: int substr(char s1, char s2) Returns 1 if s2 is a substring of s1, else it returns 0. I have written this program, but Im sure there is an...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
5
by: btober | last post by:
I can't seem to get right the regular expression for parsing data like these four sample rows (names and addresses changed to ficticious values) from a text-type column: Yolanda Harris, 38, of...
4
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
11
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.