Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

In VB6, I loved how I could determine if I wanted the Instr and Replace functions to be case-sensitive based on what I passed to the "Compare" parameter. With .NET, I know there's a .IndexOf and .Replace function for strings, but I don't know how I can make them not be case-sensitive without creating my own string function (once again!) to do some Upper or Lowercase temp string manipulation.

 

Am I missing something, or is that the only way to do it with VB.NET? (Short of just using the Microsoft.VisualBasic namespace and being done with it.)

Posted
I'm confused. How would I use String.Compare to manipulate Replace and IndexOf to be case-insensitive? Isn't .Compare it's own function that does it's own thing?
Posted

you could make a string to compare like so

 

strSomething = strStringofwhatcomesin.toLower

or

 

strSomething = strStringofwhatcomesin.toUpper

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

ok try this

strSomething = strStringofwhatcomesin.toLower.Replace("a", "whatever")

or

 

strSomething = strStringofwhatcomesin.toUpper.Replace("A", "whatever")

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

I don't have .NET on this machine to test, but I'm sure that wouldn't work since it would change the entire string to a different case.

 

I think I'm just going to have to use the Microsoft.VisualBasic Namespace for this one, rather than bother writing my own string manipulation function since I don't see a simple one line solution for this.

Posted
A regular expression replace function with regexoptions.ignorecase may provide the solution you are looking for. Check the regular expression forum.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted

Regular expressions are a pattern matching tool - see the regular expressions forum and look up via MSDN for more detailed info.

They are more flexible than the indexof and replace functions within the string class and are obtained from the System.text.RegularExpressions namespace.

 

Briefly you use them like this

regex myregex=new regex("wordtomatch",regexoptions.ignorecase);
string stringToChange="A bunch of CASE inSensItve text containing WORDtoMaTCh amongst other things";
stringToChange=myregex.Replace(stringToChange,"changedword");
Console.WriteLine(stringToChange);

Will output "A bunch of CASE inSensItve text containing changedword amongst other things"

 

Plus is able to do more powerful matching and replacing than indexof & replace.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted

Thanks, Afraits. That appears to work great for the Replace command, with me being able to decide when it should or shouldn't be case sensitive.

 

So how would you use a regular expression to mimic the IndexOf command? The only way I can find is by using the reg.Split function to return an array, then to check if it has more than 1 element. While it seems to work, it feels a little sloppy to declare a temporary array that could be several elements big, but maybe it's not a big deal. Curious to know your thoughts.

Posted
A regex.match object has an Index property which is "The position in the original string where the first character of the captured substring was found" - ie exactly what indexof would return.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted
Right, but what I'm saying is that you shouldn't necessarily be looking to replace a single function when you might be better served by changing the algorithm.
"Who is John Galt?"
Posted

True, a lot does depend on what you are trying to do and why you want to do it a particular way. In my first post I almost added in a proviso that using regex may be overkill but then decided not to :p If you need to do a case insensitive replace on a string while leaving the rest of the text unchanged then regular expressions are the only way I know. However why this is necessary I'm not sure. So your point is valid and perhaps JDYoder could tell us exactly why changing the case of the rest of the string is a no go.

 

I would also add that using the stringvar.ToLower().IndexOf (or ToUpper) would be fine if all you are after is a position within the string.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted (edited)

I'll try that out, Afraits. Thanks for the ideas. :)

 

As for why I want to do it, it really is as straight forward as I've said. The short story is, it takes user input that can be in any case, and I need to replace certain words or find where other words are located.

 

That's it. Which is simply using Replacing and IndexOf, but ignoring the lettercase.

Edited by JDYoder
  • *Experts*
Posted

First off, I'd use the Microsoft.VisualBasic namespace to do the compare - I know of know built in way to do this. A simple function could be coded yourself that I'm sure would be fast enough for your needs.

 

Make sure to include Microsoft.VisualBasic.DLL.

 

string s = "hello WORLD";
int i = Microsoft.VisualBasic.Strings.InStr(s, "L", Microsoft.VisualBasic.CompareMethod.Text);
int i2 = s.IndexOf("L");

 

Variable i will be 3 (remember that VB started counting at 1).

Variable i2 will be 9.

 

I think the real question everyone was trying to get out of you was, what is the end result of using IndexOf - what are you doing? You started to answer this with "takes user input that can be in any case, and I need to replace certain words" but that's like putting the comment "Incremet i" for the line of code i++.

 

What is the nature of the string you're replacing? For example, is this data from a file or something a user types? Are you doing a bad word filter, or something like a code lookup (replace HOND with Honda)? No one uses IndexOf just because they like knowing indexes of characters in strings - they're trying to accomplish something "bigger".

 

For example, using IndexOf usually means locating a piece of a string inside of another string. A regular expression can do this for you pretty easily. A sample:

Suppose you have a string like "ABC, 123, Hello World" and you want to get each of these strings out. You COULD use IndexOf to find each comma. You might also use the Split function. You could also use Regular expressions to get out each chunk in a named group - think of it like a small data parser that gives you a DataTable with columns. It's slower, but would be much more readable in the end (and that means more maintanable).

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
First off' date=' I'd use the Microsoft.VisualBasic namespace to do the compare - I know of know built in way to do this.[/quote']

 

 

That's what I was doing, but given all the hatred people have here toward the MS.VB namespace, I've been working to avoid it. So I was looking to replace all our existing Instr and Replace functions we have now. Regular expressions appear like they'll work just fine.

 

 

 

I think the real question everyone was trying to get out of you was' date=' what is the end result of using IndexOf - what are you doing?... What is the nature of the string you're replacing? For example, is this data from a file or something a user types?[/quote']

 

 

I realize that, but it wasn't worth going into details because it's less about one scenerio, and more about me writing a single function for our development team to use across the board for all our IndexOf and Replace needs, which will allow for an optional parameter to ignore lettercase in certain scenerios as various programmers require. The example I gave above was just one that I've come across for Replace. (Haven't needed it yet for IndexOf / Instr, but now I'll have it available for when I, or anyone else, needs it.)

Posted

There was a big discusion about the Microsoft.Visualbasic Namespace.

 

The real conclusion was, try to avoid everything that is as easyly done without it.

 

But when there is no real solution in the rest of the FW use the MS.VB functions.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...