IndexOf(string array) Can it be done?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a piece of code where a need to look for specific patterns in a
string. Because there are over 20 patterns I want to place these in a string
array for quick access

if(myValue.IndexOf(myArray)!=-1)
{
//Replace this pattern with a value from another array
}

I tried doing this in a 2 dim array, where one dimension was the pattern and
the other was the replace value. But using FOREACH(string s in myArray) I
couldn’t get it to iterate though the collection.

However I see indexof only accept char arrays. How can I better accomplish
this without a ton of IF statements
 
JP said:
I have a piece of code where a need to look for specific patterns in a
string. Because there are over 20 patterns I want to place these in a string
array for quick access

if(myValue.IndexOf(myArray)!=-1)
{
//Replace this pattern with a value from another array
}

I tried doing this in a 2 dim array, where one dimension was the pattern and
the other was the replace value. But using FOREACH(string s in myArray) I
couldn?t get it to iterate though the collection.

However I see indexof only accept char arrays. How can I better accomplish
this without a ton of IF statements

It's not entirely clear to me what you're after.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
string myValue = "A really big long ugly string";

System.Collections.Specialized.NameValueCollection oDictionary = new
System.Collections.Specialized.NameValueCollection();

oDictionary.Add("big", "HUGE");

oDictionary.Add("ugly", "FUGLY");

foreach (string sKey in oDictionary.Keys)

{

if (myValue.IndexOf(sKey) != -1)

{

//Replace this pattern with a value from another array

myValue = myValue.Replace(sKey, oDictionary.Get(sKey));

}

}

MessageBox.Show(myValue);
 
If I understand you correctly:

string[,] testValues = new string[,] { {"HUGE", "BIG"}, {"UGLY", "BAD"} };
for (int i = 0 i < testValues.GetLenth(0); i++)
{
if myValue.IndexOf(testValues[0] > -1)
// do something with textValues[1]
}

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

Gary Stewart said:
string myValue = "A really big long ugly string";

System.Collections.Specialized.NameValueCollection oDictionary = new
System.Collections.Specialized.NameValueCollection();

oDictionary.Add("big", "HUGE");

oDictionary.Add("ugly", "FUGLY");

foreach (string sKey in oDictionary.Keys)

{

if (myValue.IndexOf(sKey) != -1)

{

//Replace this pattern with a value from another array

myValue = myValue.Replace(sKey, oDictionary.Get(sKey));

}

}

MessageBox.Show(myValue);
 
<snip>

Well, that wasn't actually a complete program, but anyway, it appears
to work. Here's your code (more or less) in a complete program - it
appears to have the expected output. What doesn't it do that you want
it to do?

using System;
using System.Collections.Specialized;

class Test
{
static void Main()
{
string myValue = "A really big long ugly string";

NameValueCollection dictionary = new NameValueCollection();

dictionary["big"] = "HUGE";
dictionary["ugly"] = "FUGLY";

foreach (string key in dictionary.Keys)
{
if (myValue.IndexOf(key) != -1)
{
//Replace this pattern with a value from another array
myValue = myValue.Replace(key, dictionary[key]);
}
}

Console.WriteLine (myValue);
}
}
 
Thats because it was the answer to his question and not a reply to yours ...

Sorry for the confusion ...

Gary
 
Back
Top