473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

IndexOf(string array) Can it be done?

JP
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
..NET Software Develper
Mar 31 '06 #1
6 1521
JP <JP@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #2
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);



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JP <JP@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 31 '06 #3
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[i][0] > -1)
// do something with textValues[i][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" <GS******@NONOSPAMMicroendeavors.com> wrote in message
news:uv**************@tk2msftngp13.phx.gbl...
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);



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JP <JP@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Mar 31 '06 #4
Gary Stewart <GS******@NONOSPAMMicroendeavors.com> wrote:

<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);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #5
Thats because it was the answer to his question and not a reply to yours ...

Sorry for the confusion ...

Gary

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Gary Stewart <GS******@NONOSPAMMicroendeavors.com> wrote:

<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);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 31 '06 #6
JP
Thanks guys you have been a real big help :)

<snip>
--
JP
..NET Software Develper
Mar 31 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
2
by: John Bailo | last post by:
How come String does not have an indexOf method ?
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
4
by: sh | last post by:
The IndexOf string method returns the position of the first occurrence of a character in a string. Is there a method to find the x occurrence of a character, for example, the 28th occurrence of a...
5
by: jason | last post by:
Though this code appears to work, I suspect it could be streamline. Total Noob Here. public bool isit(string c1) { string color1 = "blue green red"; Regex re = new Regex("@"+c1,...
5
by: Olly | last post by:
Hello Group: I'd be grateful to anyone who can help me with Javascript form. :-) I want users to submit a web form, but to stop the form submission if any of my text fields contain specific...
8
by: thunderbolt | last post by:
Hi, How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005 (Dec)" in C#? The number within the brackets represent month.. am new to c# and breaking head for couple of hours over...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.