954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

extracting sustrings from a string and store it in an array

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

ansari.wajid
Light Poster
47 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 
int openBracket = 0;
int closeBracket = 0;
ArrayList subs = new ArrayList();
//you'll want to use a loop here to check
//if there are no more matches

//gets indexes
openBracket = str.IndexOf("[", openBracket);
closeBracket = str.IndexOf("]", openBracket);

//add substring
//i think you need the + 1 to include the ']'
subs.add(str.substring(openBracket, closeBracket - openBracket + 1));

//resets position
openBracket = closeBracket;
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You