Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

string manipulation in C#

funkyjazzfunkyjazz Posts: 5Member
hi every one i am here with another problem hope my friends here can help me out.

i am completely new to this language called C# and i want to do some string manipulation in this like: if want user to enter a source string then asks user to enter another string that he wants to find out then tell him if the string exits or not. search should be case sensitive and should tell the number of orrcurences plus also enable the word search.
string example: string1
word example: string1 string2

so plz if any one can come up with the practical implementation of this problem in C#(console application) i'll be really gratefull or other wise plz tell me any good websites from where i can access the detail turorials.
thanks.








Comments

  • emainoemaino Posts: 48Member
    I can easily show you code for this example but I am just looking for a little more info. So let me see if I understand this right.. You want the program to work like the following.

    Please enter a string: a

    Please enter a string to search: ab AB cia b

    Your string "a" was found: 2 times


    Is this similar to what you are looking for?

    [size=3][italic]Eric Maino[/italic][/size]
    [b][blue]GVSU[/blue] Microsoft SA[/b]

  • funkyjazzfunkyjazz Posts: 5Member
    yes i want some thing like that.

    enter source string: this is a cat

    enter a string to search: is

    your string "is" was found 1 times

    and if i enter
    IS

    your string was not found (means i want to implement case sensitive search)

    plus if i want to find a word
    enter a word to be searched: this is

    the word u enter was found

    this is what i want so hope u can help me out here. i want it in C# CONSOLE APPLICATION
    hope u can help me :)







    : I can easily show you code for this example but I am just looking for a little more info. So let me see if I understand this right.. You want the program to work like the following.
    :
    : Please enter a string: a
    :
    : Please enter a string to search: ab AB cia b
    :
    : Your string "a" was found: 2 times
    :
    :
    : Is this similar to what you are looking for?
    :
    : [size=3][italic]Eric Maino[/italic][/size]
    : [b][blue]GVSU[/blue] Microsoft SA[/b]
    :
    :

  • emainoemaino Posts: 48Member
    Here is a quick example of a solution that I think you are looking for.

    [code]
    static void Main(string[] args)
    {
    string haystack;
    string needle;

    Console.Write("Enter source string: ");
    haystack = Console.ReadLine();
    Console.Write("
    Enter a string to search: ");
    needle = Console.ReadLine();
    Console.WriteLine("");

    Regex searcher = new Regex(needle, RegexOptions.Singleline);
    Match match = searcher.Match(haystack);

    if (match.Success)
    {
    if (new Regex(@"s", RegexOptions.Singleline).Match(needle).Success)
    Console.WriteLine("The string "" + needle + "" was found");
    else
    Console.WriteLine("The string, " + needle + ", was found " + match.Captures.Count + " times.");
    }
    else
    Console.WriteLine("I am sorry but your string was not found");

    Console.WriteLine("
    --Please Press Enter to Continue--");
    Console.ReadLine();


    }
    [/code]

    Here is are some links to help out with the regular expressions if you are not familar with them. You will also need to at the following line at the top of you class file.
    [code]using System.Text.RegularExpressions;[/code]

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcomregularexpressions.asp

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconregularexpressionexamples.asp

    [size=3][italic]Eric Maino[/italic][/size]
    [b][blue]GVSU[/blue] Microsoft SA[/b]

  • funkyjazzfunkyjazz Posts: 5Member
    hey thankyou so much its working perfectly except for one thing its not showing to count of multiple enteries
    like if i enter " this is me me"

    search string: me

    your string "me" found i times though i have written it twice.

    else every thing is perfect thanks for the links they will surely help me to learn this language. if u can tell me some more links from where i can find turorials on C# then it will be more helpful.
    thanks.



  • emainoemaino Posts: 48Member
    Sorry about that. There was an error in the code. Here is one way that fixes it.
    [code]
    static void Main(string[] args)
    {
    string haystack;
    string needle;

    Console.Write("Enter source string: ");
    haystack = Console.ReadLine();
    Console.Write("
    Enter a string to search: ");
    needle = Console.ReadLine();
    Console.WriteLine("");

    Regex searcher = new Regex(needle, RegexOptions.Singleline);
    Match match = searcher.Match(haystack);

    if (match.Success)
    {
    if (new Regex(@"s", RegexOptions.Singleline).Match(needle).Success)
    Console.WriteLine("The string "" + needle + "" was found");
    else
    {
    int i = 0;
    while (match.Success)
    {
    i ++;
    match = match.NextMatch();
    }
    Console.WriteLine("The string, " + needle + ", was found " + i + " times.");
    }
    }
    else
    Console.WriteLine("I am sorry but your string was not found");

    Console.WriteLine("
    --Please Press Enter to Continue--");
    Console.ReadLine();


    }
    [/code]

    I will also post some more links later.


    [size=3][italic]Eric Maino[/italic][/size]
    [b][blue]GVSU[/blue] Microsoft SA[/b]

  • uoc1691uoc1691 Posts: 1Member
    Hi Eric,

    Thanks for this great and useful post.

    here what if we want to search more than one word in a give set of words. Say i would want find out how many times the phrase "you have" is stated in the below para:

    "Before you start using style-codes, please note that you have to close ALL tags.Please use the preview function to make sure that your message is shown as you want it to be."

    Can you please show us how to do this? Thanks
    Cheers!
Sign In or Register to comment.