943,852 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 926
  • ASP.NET RSS
Sep 16th, 2008
0

extracting sustrings from a string and store it in an array

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
ansari.wajid is offline Offline
47 posts
since Aug 2007
Sep 16th, 2008
0

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

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: working with GRIDVIEW
Next Thread in ASP.NET Forum Timeline: using common dialogbox in asp.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC