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
-
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
Look at String.IndexOf http://msdn.microsoft.com/en-us/library/system.string.indexof(VS.80).aspx[^] and String.Substring http://msdn.microsoft.com/en-us/library/system.string.substring(VS.80).aspx[^].
-
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
string.Split method.
string str = "something is [!better] than [!nothing]";
string[] separators = new string[] { "[!better]", "[!nothing]" };
string[] strArray = str.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in strArray)
{
Console.WriteLine(item.Trim());
}Edit: Just reread the question and you want the exact opposite! :-O :confused:
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
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
It would probably end up being a three-step process. If it's safe to assume that the stuff you're looking for would always be separated by a space, you could do this:
string str = "something is [!better] than [!nothing]";
// split the string on spaces
string[] parts = str.Split(' ');
List resultStringList = new List();foreach (string part in parts)
{
int pos1 = part.IndexOf("[");
int pos2 = part.IndexOf("]");
if (pos1 >= 0 && pos2 >= 0)
{
// get everything between the brackets, but NOT the brackets themselves
string result = part.SubString(pos1+1, pos2);
// add it to the list
resultStringList.Add(result);
}
}string[] myResults = new string[resultStringList.Count];
for (int i = 0; i < resultStringList.Count; i++)
{
myResults[i] = resultStringList[i];
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Tuesday, September 16, 2008 10:23 AM
-
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
Regular Expressions?
-
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
How about a one-liner to extract the strings from the array using a regular expression and placing them in an array? :)
string[] substrings = Regex.Matches(str, @"\[.*?\]").OfType<match>().Select(m => m.Value).ToArray();
Despite everything, the person most likely to be fooling you next is yourself.
modified on Tuesday, September 16, 2008 12:18 PM
-
How about a one-liner to extract the strings from the array using a regular expression and placing them in an array? :)
string[] substrings = Regex.Matches(str, @"\[.*?\]").OfType<match>().Select(m => m.Value).ToArray();
Despite everything, the person most likely to be fooling you next is yourself.
modified on Tuesday, September 16, 2008 12:18 PM
-
It would probably end up being a three-step process. If it's safe to assume that the stuff you're looking for would always be separated by a space, you could do this:
string str = "something is [!better] than [!nothing]";
// split the string on spaces
string[] parts = str.Split(' ');
List resultStringList = new List();foreach (string part in parts)
{
int pos1 = part.IndexOf("[");
int pos2 = part.IndexOf("]");
if (pos1 >= 0 && pos2 >= 0)
{
// get everything between the brackets, but NOT the brackets themselves
string result = part.SubString(pos1+1, pos2);
// add it to the list
resultStringList.Add(result);
}
}string[] myResults = new string[resultStringList.Count];
for (int i = 0; i < resultStringList.Count; i++)
{
myResults[i] = resultStringList[i];
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Tuesday, September 16, 2008 10:23 AM
-
Hi Guffa Thanks for your reply. I am getting "Invalid expression term '>'" error. Please help resolve this issue. Thanks Ansari
Hi After doing r&d on regular expressions I got this answer. If we want to extract a word from a string like "[something]" We could use the Match method in the regular expression. Like this: string s = "
[!vDescription][!vDate]
[!vTitle]
"; Regex rx = new Regex(@"\[.*?\]"); Match mc = rx.Match(s); while(mc.Success) { Response.Write(mc.Value.ToString()); mc = mc.NextMatch(); } Thanks
-
Hi Guffa Thanks for your reply. I am getting "Invalid expression term '>'" error. Please help resolve this issue. Thanks Ansari
-
Hi After doing r&d on regular expressions I got this answer. If we want to extract a word from a string like "[something]" We could use the Match method in the regular expression. Like this: string s = "
[!vDescription][!vDate]
[!vTitle]
"; Regex rx = new Regex(@"\[.*?\]"); Match mc = rx.Match(s); while(mc.Success) { Response.Write(mc.Value.ToString()); mc = mc.NextMatch(); } Thanks
Use the Matches method to get them all:
MatchCollection matches = Regex.Matches(s, "\[.*?\]");
string substrings[] = new string[Matches.Count];
for (int i = 0; i < matches.Count; i++) {
substrings[i] = matches[i].Value;
}Despite everything, the person most likely to be fooling you next is yourself.
-
Use the Matches method to get them all:
MatchCollection matches = Regex.Matches(s, "\[.*?\]");
string substrings[] = new string[Matches.Count];
for (int i = 0; i < matches.Count; i++) {
substrings[i] = matches[i].Value;
}Despite everything, the person most likely to be fooling you next is yourself.
Hi Guffa My requirement changed, I am sure you can help me. I have a string for example str="something [!str1] and something [!str2].." so I want to store the substring from start up till "[" and then from "[" to "]" and so on. so how can we achieve it. Thanks Ansari
-
Hi Guffa My requirement changed, I am sure you can help me. I have a string for example str="something [!str1] and something [!str2].." so I want to store the substring from start up till "[" and then from "[" to "]" and so on. so how can we achieve it. Thanks Ansari
Split on brackets, then you get an array containing the strings between the brackets.
string str="something [!str1] and something [!str2] else."; string[] substrings = str.Split(new char[]{ '[', ']' });
This gives you an array with the following strings:"something " "!str1" " and something " "!str2" " else."
Despite everything, the person most likely to be fooling you next is yourself.
-
Split on brackets, then you get an array containing the strings between the brackets.
string str="something [!str1] and something [!str2] else."; string[] substrings = str.Split(new char[]{ '[', ']' });
This gives you an array with the following strings:"something " "!str1" " and something " "!str2" " else."
Despite everything, the person most likely to be fooling you next is yourself.