extracting sustrings from a string and store it in an array

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 33
Reputation: ansari.wajid is an unknown quantity at this point 
Solved Threads: 0
ansari.wajid ansari.wajid is offline Offline
Light Poster

extracting sustrings from a string and store it in an array

 
0
  #1
Sep 16th, 2008
Hi

I want to extract sustrings from a string and want to store in an array.
The string is dynamic it will change but the criteria to extract substring is same.
say for example a string str="something is [!better] than [!nothing]"
so i need to extract [!better] and [!nothing] from the string and store it in a string array, the string is dynamic it changes but i need to extract data between [ and ] including both the square brackets.
If anybody can send any sample code, I will be very thankfull.
Thanks

Ansari
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: extracting sustrings from a string and store it in an array

 
0
  #2
Sep 16th, 2008
  1. int openBracket = 0;
  2. int closeBracket = 0;
  3. ArrayList subs = new ArrayList();
  4. //you'll want to use a loop here to check
  5. //if there are no more matches
  6.  
  7. //gets indexes
  8. openBracket = str.IndexOf("[", openBracket);
  9. closeBracket = str.IndexOf("]", openBracket);
  10.  
  11. //add substring
  12. //i think you need the + 1 to include the ']'
  13. subs.add(str.substring(openBracket, closeBracket - openBracket + 1));
  14.  
  15. //resets position
  16. openBracket = closeBracket;
Last edited by dickersonka; Sep 16th, 2008 at 11:50 am.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC