Results 1 to 9 of 9

Thread: String searching

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. string hey = "hello";
    2.                 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Addicted Member corwin_ranger's Avatar
    Join Date
    Sep 2004
    Location
    CT
    Posts
    198

    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();
    		}
    	}
    }

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: String searching

    sure. i understand and am aware of this but apperently they dont care about performance....typical.
    but yeh

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: String searching

    Too few people do these days.

  8. #8
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    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"));

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width