473,654 Members | 3,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Inde xOf(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 1532
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.Inde xOf(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.co m>
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.Collecti ons.Specialized .NameValueColle ction oDictionary = new
System.Collecti ons.Specialized .NameValueColle ction();

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

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

foreach (string sKey in oDictionary.Key s)

{

if (myValue.IndexO f(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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.Inde xOf(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.co m>
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.GetL enth(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******@NONOS PAMMicroendeavo rs.com> wrote in message
news:uv******** ******@tk2msftn gp13.phx.gbl...
string myValue = "A really big long ugly string";

System.Collecti ons.Specialized .NameValueColle ction oDictionary = new
System.Collecti ons.Specialized .NameValueColle ction();

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

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

foreach (string sKey in oDictionary.Key s)

{

if (myValue.IndexO f(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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.Inde xOf(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.co m>
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******@NONOS PAMMicroendeavo rs.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.Collecti ons.Specialized ;

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

NameValueCollec tion dictionary = new NameValueCollec tion();

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

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

Console.WriteLi ne (myValue);
}
}

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Gary Stewart <GS******@NONOS PAMMicroendeavo rs.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.Collecti ons.Specialized ;

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

NameValueCollec tion dictionary = new NameValueCollec tion();

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

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

Console.WriteLi ne (myValue);
}
}

--
Jon Skeet - <sk***@pobox.co m>
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
21463
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 'column' in the ArrayList, the other is automatically sorted and so I can use IndexOf without fear. This is because I will be going through 20,000 some odd records and don't want to use a Lookup type of table/query; I figure an ArrayList.IndexOf method...
2
8272
by: John Bailo | last post by:
How come String does not have an indexOf method ?
7
13984
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]) and the index is the location after "." in name. First, i should check the given name is a valid float-point, and then, to check the number in name in index locaion is > 0 or not? eg:
29
4301
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 substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
18
4727
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
4
3708
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 character in a string? Thanks
5
2942
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, RegexOptions.IgnoreCase | RegexOptions.Multiline); if (re.Matches(color1).Count 0)
5
2756
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 value (e.g. "http"). I incorporated "http" detection bits of script into my existing function (only for one of my fields for now ), but for some reason this script doesn' work, probably, because of the wrong syntax. =============
8
5142
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 this seemingly simple thing, would be glad if anybody could help. Thanks in Advance, Ajai
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.