|
-
Jul 6th, 2007, 07:20 AM
#1
Thread Starter
PowerPoster
String searching
ok, even though we all should know string searching should be avoided where possible, this cannot unfortunately.
say we have these sets of strings:
temporary
permenant
temporary/permenant
contract
contract/temporary
and we are testing with a string called "perm".
how can I check to see if each one them contains that word? The complication is this:
permenant and temporary/permenant SHOULD evaluate to true, since "temporary/permenant" contains the word "perm"
I am using .NET 1.1 here for this project.
-
Jul 6th, 2007, 07:59 AM
#2
Re: String searching
The Contains method would be suitable in this case, but since that is a new method in the .Net 2.0 framework, you could use the IndexOf method
C# Code:
string hey = "hello";
hey.IndexOf("e"); //will return 1, because e exists on index 1 in the string.
If IndexOf returns -1, the substring doesnt exist within the string.
-
Jul 6th, 2007, 08:07 AM
#3
Addicted Member
Re: String searching
You'll want to use the String.Contains() method. It's not static, so you'll need to do something like what's below. You can obviously do something more efficient based on your needs. I also included to the .ToLower() method since you may not know if your test string is all upper or lower case.
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a = "temporary";
string b = "permenant";
string c = "temporary/permenant";
string d = "contract";
string e = "contract/temporary";
string test = "perm";
if (a.ToLower().Contains(test))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
if (b.ToLower().Contains(test))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
if (c.ToLower().Contains(test))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
if (d.ToLower().Contains(test))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
if (e.ToLower().Contains(test))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
Console.ReadLine();
}
}
}
-
Jul 6th, 2007, 08:58 AM
#4
Thread Starter
PowerPoster
Re: String searching
.NET 1.1 does not have the contains method. however I did find a solution.
simply did a .Split() on the string then went through each item in the array, got the first 4 characters and did a comparison (indexof) with the other string which had 4 characters - seems to work just fine, thankfully
-
Jul 6th, 2007, 12:58 PM
#5
Re: String searching
Split is one of the most appallingly slow functions in the whole .net library.
If you are really interested in optimisation, learn how to use pointers and develop a custom algorithm to suit your exact requirement for this task. It will be much more efficient and won't double your memory usage (like Split will).
I don't live here any more.
-
Jul 6th, 2007, 01:06 PM
#6
Thread Starter
PowerPoster
Re: String searching
sure. i understand and am aware of this but apperently they dont care about performance....typical.
but yeh
-
Jul 6th, 2007, 01:53 PM
#7
Re: String searching
Too few people do these days.
-
Jul 7th, 2007, 04:55 PM
#8
Fanatic Member
Re: String searching
You could use Regular Expressions.
Code:
Regex regex = new Regex("perm", RegexOptions.IgnoreCase);
Console.WriteLine(regex.IsMatch("temporary"));
Console.WriteLine(regex.IsMatch("permenant"));
Console.WriteLine(regex.IsMatch("temporary/permenant"));
-
Jul 7th, 2007, 05:16 PM
#9
Thread Starter
PowerPoster
Re: String searching
could do but as well as it being expensive, it is the right method to use however I guess I need a regex expression to look for the criterias:
temp/perm
permenant
perm/temp
so it needs to look for "perm" and bring back temp/perm, permenant and perm/temp
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|