473,378 Members | 1,555 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,378 software developers and data experts.

C# String Comparison, IndexOf and Related

Hi Everyone,

I've been looking through these .NET groups and can't find the exact
answer I want, so I'm asking.

Can someone let me know the best way (you feel) to search a C# string
for an occurance of a CASE INSENSITIVE substring, returning the found
position. I'm speaking of larger strings to search as well ~50K-500K.
Here's what I have so far:

* ToUpper/ToLower and IndexOf would be quite slow, right? as strings
are immutable and these search strings are larger to begin with.

* RegEx could be the answer, but I'm not sure pattern matching would
be the right solution for this problem

* Any unsafe code, Boyer-Moore using pointers or inline assembly (if
that's possible), would seem the best, but well, it's unsafe code

* I've found a MapTable example here in the C# nj (thanks maptable
person), and think this might be the best solution

Any help is appreciated, thanks in advance!
BILL
Nov 16 '05 #1
5 14893
Bill,
I did some tests. I created a 5 MB file and loaded it into a
streamreader. I assigned all of the text from the file into a string object.
I did a tolower and it returned the index of the specified substring
immediately. I also used some of the globalization classes that allows you
to do indexof with an ignorecase parameter. That also returned the index
immediately. I don't have any numbers as far as time that it took to run but
during debugging it literally stepped over the line of code doing the
comparison with no pause whatsoever.

here is the globalization code. I used a very simple text comparison below.

CultureInfo culture = new CultureInfo("en-us");

int index = culture.CompareInfo.IndexOf("this is a
TEST","test",System.Globalization.CompareOptions.I gnoreCase);

HTH
--
Lateralus [MCAD]
"BILL" <ti******@yahoo.com> wrote in message
news:cd**************************@posting.google.c om...
Hi Everyone,

I've been looking through these .NET groups and can't find the exact
answer I want, so I'm asking.

Can someone let me know the best way (you feel) to search a C# string
for an occurance of a CASE INSENSITIVE substring, returning the found
position. I'm speaking of larger strings to search as well ~50K-500K.
Here's what I have so far:

* ToUpper/ToLower and IndexOf would be quite slow, right? as strings
are immutable and these search strings are larger to begin with.

* RegEx could be the answer, but I'm not sure pattern matching would
be the right solution for this problem

* Any unsafe code, Boyer-Moore using pointers or inline assembly (if
that's possible), would seem the best, but well, it's unsafe code

* I've found a MapTable example here in the C# nj (thanks maptable
person), and think this might be the best solution

Any help is appreciated, thanks in advance!
BILL

Nov 16 '05 #2
Thanks Lateralus - although I was a bit skeptical of the results,
after doing similar tests, I think I've changed my thinking on the
matter. I ran some IndexOf/ToUpper and related code on a few older
boxes I have here (eg, 500Mhz AMD, 512M) and didn't see any real
performance degradation either.

So - here's my question to everyone- if I'm not looking to do
heavy-duty work with these strings I think I'm best off using .NET
methods. The original question might have resulted from my being
trained as an anal-C++-guy, if so ... sorry all :)
"Lateralus [MCAD]" <dnorm252_at_yahoo.com> wrote in message news:<eL**************@TK2MSFTNGP09.phx.gbl>...
Bill,
I did some tests. I created a 5 MB file and loaded it into a
streamreader. I assigned all of the text from the file into a string object.
I did a tolower and it returned the index of the specified substring
immediately. I also used some of the globalization classes that allows you
to do indexof with an ignorecase parameter. That also returned the index
immediately. I don't have any numbers as far as time that it took to run but
during debugging it literally stepped over the line of code doing the
comparison with no pause whatsoever.

here is the globalization code. I used a very simple text comparison below.

CultureInfo culture = new CultureInfo("en-us");

int index = culture.CompareInfo.IndexOf("this is a
TEST","test",System.Globalization.CompareOptions.I gnoreCase);

HTH
--
Lateralus [MCAD]
"BILL" <ti******@yahoo.com> wrote in message
news:cd**************************@posting.google.c om...
Hi Everyone,

I've been looking through these .NET groups and can't find the exact
answer I want, so I'm asking.

Can someone let me know the best way (you feel) to search a C# string
for an occurance of a CASE INSENSITIVE substring, returning the found
position. I'm speaking of larger strings to search as well ~50K-500K.
Here's what I have so far:

* ToUpper/ToLower and IndexOf would be quite slow, right? as strings
are immutable and these search strings are larger to begin with.

* RegEx could be the answer, but I'm not sure pattern matching would
be the right solution for this problem

* Any unsafe code, Boyer-Moore using pointers or inline assembly (if
that's possible), would seem the best, but well, it's unsafe code

* I've found a MapTable example here in the C# nj (thanks maptable
person), and think this might be the best solution

Any help is appreciated, thanks in advance!
BILL

Nov 16 '05 #3
Bill,
I can understand where youre coming from. Whenever our applications need
heavy string manipulation on large amounts of data we would always write the
dll in C++. There is nothing scientific about my next statement because I
never ran any "true" tests. We had a c++ dll that would manipulate large
strings up to 10MB in size. When it was rewritten in c# we didn't notice any
degredation in the speed becides it's first time executing since it gets
compiled the first time. So basically I found that the systems I've worked
on there is no need to turn to C++ as there was in the past. Of course there
are going to be times that you will need to, but for this one I think you're
ok with C#.

--
Lateralus [MCAD]
"BILL" <ti******@yahoo.com> wrote in message
news:cd**************************@posting.google.c om...
Thanks Lateralus - although I was a bit skeptical of the results,
after doing similar tests, I think I've changed my thinking on the
matter. I ran some IndexOf/ToUpper and related code on a few older
boxes I have here (eg, 500Mhz AMD, 512M) and didn't see any real
performance degradation either.

So - here's my question to everyone- if I'm not looking to do
heavy-duty work with these strings I think I'm best off using .NET
methods. The original question might have resulted from my being
trained as an anal-C++-guy, if so ... sorry all :)
"Lateralus [MCAD]" <dnorm252_at_yahoo.com> wrote in message
news:<eL**************@TK2MSFTNGP09.phx.gbl>...
Bill,
I did some tests. I created a 5 MB file and loaded it into a
streamreader. I assigned all of the text from the file into a string
object.
I did a tolower and it returned the index of the specified substring
immediately. I also used some of the globalization classes that allows
you
to do indexof with an ignorecase parameter. That also returned the index
immediately. I don't have any numbers as far as time that it took to run
but
during debugging it literally stepped over the line of code doing the
comparison with no pause whatsoever.

here is the globalization code. I used a very simple text comparison
below.

CultureInfo culture = new CultureInfo("en-us");

int index = culture.CompareInfo.IndexOf("this is a
TEST","test",System.Globalization.CompareOptions.I gnoreCase);

HTH
--
Lateralus [MCAD]
"BILL" <ti******@yahoo.com> wrote in message
news:cd**************************@posting.google.c om...
> Hi Everyone,
>
> I've been looking through these .NET groups and can't find the exact
> answer I want, so I'm asking.
>
> Can someone let me know the best way (you feel) to search a C# string
> for an occurance of a CASE INSENSITIVE substring, returning the found
> position. I'm speaking of larger strings to search as well ~50K-500K.
> Here's what I have so far:
>
> * ToUpper/ToLower and IndexOf would be quite slow, right? as strings
> are immutable and these search strings are larger to begin with.
>
> * RegEx could be the answer, but I'm not sure pattern matching would
> be the right solution for this problem
>
> * Any unsafe code, Boyer-Moore using pointers or inline assembly (if
> that's possible), would seem the best, but well, it's unsafe code
>
> * I've found a MapTable example here in the C# nj (thanks maptable
> person), and think this might be the best solution
>
> Any help is appreciated, thanks in advance!
> BILL

Nov 16 '05 #4
Lateralus - Thanks! It's hard to leave my C++/MASM behind, but you're
->absolutely<- right, I'll attack these problems when needed now. Any
different opinions on this thread are always welcome, but I think I've
found my answer...
BILL
"Lateralus [MCAD]" <dnorm252_at_yahoo.com> wrote in message news:<#W**************@TK2MSFTNGP15.phx.gbl>...
Bill,
I can understand where youre coming from. Whenever our applications need
heavy string manipulation on large amounts of data we would always write the
dll in C++. There is nothing scientific about my next statement because I
never ran any "true" tests. We had a c++ dll that would manipulate large
strings up to 10MB in size. When it was rewritten in c# we didn't notice any
degredation in the speed becides it's first time executing since it gets
compiled the first time. So basically I found that the systems I've worked
on there is no need to turn to C++ as there was in the past. Of course there
are going to be times that you will need to, but for this one I think you're
ok with C#.

--
Lateralus [MCAD]

<snip>
Nov 16 '05 #5
BILL <ti******@yahoo.com> wrote:
I've been looking through these .NET groups and can't find the exact
answer I want, so I'm asking.

Can someone let me know the best way (you feel) to search a C# string
for an occurance of a CASE INSENSITIVE substring, returning the found
position. I'm speaking of larger strings to search as well ~50K-500K.
Here's what I have so far:


<snip>

In addition to the previous comments, you may wish to consider using
CompareInfo.IndexOf (source, value, CompareOptions.IgnoreCase)

You can get a CompareInfo reference from a CultureInfo - you could use
the current culture (CultureInfo.CurrentCulture) or the invariant one
(CultureInfo.InvariantCulture).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
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]) ...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
17
by: Dinsdale | last post by:
I would like to compare a string value to a pre-determined list of other strings. Is there a simple way to do this in one statements like this: if(strMystring.ToUpper() == ("STRING1"| "STRING2"|...
8
by: shapper | last post by:
Hello, I have a string which holds a text. Is it possible to create a substring which uses the first N words of that string? Thanks, Miguel
1
by: dhtmlkitchen | last post by:
Why does "foo" instanceof String return false? It just seems counterintuitive to me. I mean, sure, I can always check the constructor: var fooVar = "foo"; var isString =...
8
by: SMJT | last post by:
Does anyone know why the string contains function always returns true if the token is an empty string? I expected it to return false. "AnyOldText".Contains("") or...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.