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
Loading
AlanPosted Sep 17, 2008, 5:07 AM
OK, here's a revised version which can deal with any number of substrings:
using System;
using System.Collections.Generic;
class Program list = new List();
{
static void Main()
{
List
string str = "something is [!better] than [!nothing]";
list.AddRange(ExtractSubstrings(str));
string str2 = "something [!is] better [!than] [!nothing]";
list.AddRange(ExtractSubstrings(str2));
string[] results = list.ToArray();
// check it worked
foreach(string result in results)
{
Console.WriteLine(result);
}
Console.ReadKey();
}
static List ExtractSubstrings(string str) results = new List();
{
if (str == null) return null;
List
if (str == "") return results; // empty list
int index1 = 0, index2 = 0;
bool finished = false;
do
{
index1 = str.IndexOf("[", index2);
if (index1 == - 1)
{
finished = true;
}
else
{
index2 = str.IndexOf("]", index1);
if (index2 == - 1)
{
finished = true;
}
else
{
results.Add(str.Substring(index1, index2 - index1 + 1));
}
}
}
while(!finished);
return results;
}
}
wajid ansariPosted Sep 17, 2008, 6:42 AM
your code is very helpfull.
wajid ansariPosted Sep 17, 2008, 12:34 AM
Thanks for your quick reply. I appreciate it your help.
One more updation in the code please, Your code seems to work to extract two substrings but if the string is "something [!is] better [!than] [!nothing]" then I need to extract three substrings but it is extracting [!is] and [!than], can you please update your code to get any number of substrings because the string that I am passing will have any number of substrings. Thanks again, Hope you will reply soon.
Thanks
Ansari
AlanPosted Sep 16, 2008, 11:10 AM
You could use regular expressions for this but I thought I'd do it using ordinary string parsing (including full error checking) which should be quicker:
using System;
using System.Collections.Generic;
class Program list = new List();
{
static void Main()
{
List
string str = "something is [!better] than [!nothing]";
string sub1, sub2;
ExtractSubstrings(str, out sub1, out sub2);
list.Add(sub1);
list.Add(sub2);
string str2 = "nothing is [!worse] than [!something]";
string sub3, sub4;
ExtractSubstrings(str2, out sub3, out sub4);
list.Add(sub3);
list.Add(sub4);
string[] results = list.ToArray();
// check it worked
foreach(string result in results)
{
Console.WriteLine(result);
}
Console.ReadKey();
}
static void ExtractSubstrings(string str, out string sub1, out string sub2)
{
if (String.IsNullOrEmpty(str))
{
sub1 = sub2 = str;
return;
}
int index1 = str.IndexOf("[");
if (index1 == - 1)
{
sub1 = sub2 = "";
return;
}
int index2 = str.IndexOf("]", index1);
if (index2 == - 1)
{
sub1 = sub2 = "";
return;
}
sub1 = str.Substring(index1, index2 - index1 + 1);
int index3 = str.IndexOf("[", index2);
if (index3 == - 1)
{
sub2 = "";
return;
}
int index4 = str.IndexOf("]", index3);
if (index4 == - 1)
{
sub2 = "";
return;
}
sub2 = str.Substring(index3, index4 - index3 + 1);
}
}