Beginners question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Hoe

    Beginners question

    Hello, I'm new to C# and I am having a small problem. All I want to do is
    take a sentence from the user then replace certain words with other ones.
    So I started off with this:

    string myString = Console.ReadLin e();
    string editString;
    editString = myString.Replac e("is", "isn't");
    Console.WriteLi ne(editString);

    Which if I entered "This is a test" I got back "Thisn't isn't a test"
    I got round this by looking for the word to replace by putting spaces round
    the string:

    string myString = Console.ReadLin e();
    string editString;
    editString = myString.Replac e(" is ", " isn't ");
    Console.WriteLi ne(editString);

    Which was fine for this example, but what if there is punctuation involved?
    what if I wanted to convert "This is." it wouldn't pick it up because of the
    full stop. Would it be best to do it a different way by stripping all the
    punctuation out somehow? but then how would you put it back in? is there a
    simple way of doing this?
    Thanks for any help,
    Andy


  • AlexS

    #2
    Re: Beginners question

    Take a look at Regex.Replace and regular expressions.
    HTH
    Alex

    "Andy Hoe" <andyhoeBLAH@ds l.pipexBLAH.com BLAH> wrote in message
    news:3fa16c0f$0 $5648$cc9e4d1f@ news.dial.pipex .com...[color=blue]
    > Hello, I'm new to C# and I am having a small problem. All I want to do is
    > take a sentence from the user then replace certain words with other ones.
    > So I started off with this:
    >
    > string myString = Console.ReadLin e();
    > string editString;
    > editString = myString.Replac e("is", "isn't");
    > Console.WriteLi ne(editString);
    >
    > Which if I entered "This is a test" I got back "Thisn't isn't a test"
    > I got round this by looking for the word to replace by putting spaces[/color]
    round[color=blue]
    > the string:
    >
    > string myString = Console.ReadLin e();
    > string editString;
    > editString = myString.Replac e(" is ", " isn't ");
    > Console.WriteLi ne(editString);
    >
    > Which was fine for this example, but what if there is punctuation[/color]
    involved?[color=blue]
    > what if I wanted to convert "This is." it wouldn't pick it up because of[/color]
    the[color=blue]
    > full stop. Would it be best to do it a different way by stripping all the
    > punctuation out somehow? but then how would you put it back in? is there a
    > simple way of doing this?
    > Thanks for any help,
    > Andy
    >
    >[/color]


    Comment

    • 100

      #3
      Re: Beginners question

      Hi Andy,

      Take a look on *Regex* class and regular expressions

      HTH
      B\rgds
      100

      "Andy Hoe" <andyhoeBLAH@ds l.pipexBLAH.com BLAH> wrote in message
      news:3fa16c0f$0 $5648$cc9e4d1f@ news.dial.pipex .com...[color=blue]
      > Hello, I'm new to C# and I am having a small problem. All I want to do is
      > take a sentence from the user then replace certain words with other ones.
      > So I started off with this:
      >
      > string myString = Console.ReadLin e();
      > string editString;
      > editString = myString.Replac e("is", "isn't");
      > Console.WriteLi ne(editString);
      >
      > Which if I entered "This is a test" I got back "Thisn't isn't a test"
      > I got round this by looking for the word to replace by putting spaces[/color]
      round[color=blue]
      > the string:
      >
      > string myString = Console.ReadLin e();
      > string editString;
      > editString = myString.Replac e(" is ", " isn't ");
      > Console.WriteLi ne(editString);
      >
      > Which was fine for this example, but what if there is punctuation[/color]
      involved?[color=blue]
      > what if I wanted to convert "This is." it wouldn't pick it up because of[/color]
      the[color=blue]
      > full stop. Would it be best to do it a different way by stripping all the
      > punctuation out somehow? but then how would you put it back in? is there a
      > simple way of doing this?
      > Thanks for any help,
      > Andy
      >
      >[/color]


      Comment

      • Jhon

        #4
        Re: Beginners question

        Hi,

        Regular expressions have a zero-width assertation \b. This specifies that
        the match must occur on a word boundary.

        using System.Text.Reg ularExpressions ;
        [color=blue]
        > string myString = Console.ReadLin e();
        > string editString;[/color]
        editString = Regex.Replace (myString,"\\bi s","isn't");[color=blue]
        > Console.WriteLi ne(editString);[/color]


        HTH
        greetings


        "Andy Hoe" <andyhoeBLAH@ds l.pipexBLAH.com BLAH> wrote in message
        news:3fa16c0f$0 $5648$cc9e4d1f@ news.dial.pipex .com...[color=blue]
        > Hello, I'm new to C# and I am having a small problem. All I want to do is
        > take a sentence from the user then replace certain words with other ones.
        > So I started off with this:
        >
        > string myString = Console.ReadLin e();
        > string editString;
        > editString = myString.Replac e("is", "isn't");
        > Console.WriteLi ne(editString);
        >
        > Which if I entered "This is a test" I got back "Thisn't isn't a test"
        > I got round this by looking for the word to replace by putting spaces[/color]
        round[color=blue]
        > the string:
        >
        > string myString = Console.ReadLin e();
        > string editString;
        > editString = myString.Replac e(" is ", " isn't ");
        > Console.WriteLi ne(editString);
        >
        > Which was fine for this example, but what if there is punctuation[/color]
        involved?[color=blue]
        > what if I wanted to convert "This is." it wouldn't pick it up because of[/color]
        the[color=blue]
        > full stop. Would it be best to do it a different way by stripping all the
        > punctuation out somehow? but then how would you put it back in? is there a
        > simple way of doing this?
        > Thanks for any help,
        > Andy
        >
        >[/color]


        Comment

        • Carol Sun

          #5
          Re: Beginners question

          Why don't you say
          editString = myString.Replac e (" is", " isn't");

          "Andy Hoe" <andyhoeBLAH@ds l.pipexBLAH.com BLAH> wrote in message news:<3fa16c0f$ 0$5648$cc9e4d1f @news.dial.pipe x.com>...[color=blue]
          > Hello, I'm new to C# and I am having a small problem. All I want to do is
          > take a sentence from the user then replace certain words with other ones.
          > So I started off with this:
          >
          > string myString = Console.ReadLin e();
          > string editString;
          > editString = myString.Replac e("is", "isn't");
          > Console.WriteLi ne(editString);
          >
          > Which if I entered "This is a test" I got back "Thisn't isn't a test"
          > I got round this by looking for the word to replace by putting spaces round
          > the string:
          >
          > string myString = Console.ReadLin e();
          > string editString;
          > editString = myString.Replac e(" is ", " isn't ");
          > Console.WriteLi ne(editString);
          >
          > Which was fine for this example, but what if there is punctuation involved?
          > what if I wanted to convert "This is." it wouldn't pick it up because of the
          > full stop. Would it be best to do it a different way by stripping all the
          > punctuation out somehow? but then how would you put it back in? is there a
          > simple way of doing this?
          > Thanks for any help,
          > Andy[/color]

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Beginners question

            Carol Sun <apry08@hotmail .com> wrote:[color=blue]
            > Why don't you say
            > editString = myString.Replac e (" is", " isn't");[/color]

            That has the same problem with other words:

            "This is an issue that needs resolving" would become
            "This isn't an isn'tsue that needs resolving"

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...