JDYoder Posted August 9, 2005 Posted August 9, 2005 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.) Quote
Administrators PlausiblyDamp Posted August 9, 2005 Administrators Posted August 9, 2005 String.Compare allows you to specify if comparisons are done case sensitive or not. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
JDYoder Posted August 9, 2005 Author Posted August 9, 2005 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? Quote
techmanbd Posted August 9, 2005 Posted August 9, 2005 you could make a string to compare like so strSomething = strStringofwhatcomesin.toLower or strSomething = strStringofwhatcomesin.toUpper Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
JDYoder Posted August 9, 2005 Author Posted August 9, 2005 But I don't want to compare one string to another. I want to use Replace and IndexOf in a way that ignores the lettercase. Quote
techmanbd Posted August 9, 2005 Posted August 9, 2005 ok try this strSomething = strStringofwhatcomesin.toLower.Replace("a", "whatever") or strSomething = strStringofwhatcomesin.toUpper.Replace("A", "whatever") Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
JDYoder Posted August 10, 2005 Author Posted August 10, 2005 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. Quote
Afraits Posted August 10, 2005 Posted August 10, 2005 A regular expression replace function with regexoptions.ignorecase may provide the solution you are looking for. Check the regular expression forum. Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
JDYoder Posted August 10, 2005 Author Posted August 10, 2005 I'm not familiar with the term "regular expression". What is it? Quote
Afraits Posted August 10, 2005 Posted August 10, 2005 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. Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
IngisKahn Posted August 10, 2005 Posted August 10, 2005 When I'm not familiar with X, I type X into my favorite search engine. ;) Quote "Who is John Galt?"
JDYoder Posted August 10, 2005 Author Posted August 10, 2005 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. Quote
IngisKahn Posted August 11, 2005 Posted August 11, 2005 Rather than replacing the IndexOf function, I'd ask what you're trying to do. Quote "Who is John Galt?"
JDYoder Posted August 11, 2005 Author Posted August 11, 2005 Uh, exactly what my first post said, which is to not have IndexOf be case sensitive. Quote
IngisKahn Posted August 11, 2005 Posted August 11, 2005 Well, IndexOf is a means to an end. As such, there's no one best method to use RegEx instead of String.IndexOf. Quote "Who is John Galt?"
Afraits Posted August 11, 2005 Posted August 11, 2005 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. Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
IngisKahn Posted August 11, 2005 Posted August 11, 2005 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. Quote "Who is John Galt?"
Afraits Posted August 11, 2005 Posted August 11, 2005 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. Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
JDYoder Posted August 11, 2005 Author Posted August 11, 2005 (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 August 11, 2005 by JDYoder Quote
*Experts* Nerseus Posted August 12, 2005 *Experts* Posted August 12, 2005 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 Quote "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
JDYoder Posted August 12, 2005 Author Posted August 12, 2005 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.) Quote
FZelle Posted August 13, 2005 Posted August 13, 2005 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.