question on substring

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

    question on substring

    How do i get rid of the "OR " (at the end) from this string?

    I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
    I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "

    Many thanks
  • Gishu

    #2
    RE: question on substring



    "huzz" wrote:
    [color=blue]
    > How do i get rid of the "OR " (at the end) from this string?
    >
    > I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
    > I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
    >
    > Many thanks[/color]

    Comment

    • Morten Wennevik

      #3
      Re: question on substring

      Hi huzz,

      Well, I'm not entirely sure this is the best approach, but if you know
      there is "OR " at the end you could do

      String s = "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR ";
      s = s.Substring(0, s.Length - "OR ".Length);


      --
      Happy Coding!
      Morten Wennevik [C# MVP]

      Comment

      • Dave

        #4
        Re: question on substring

        > I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "[color=blue]
        > I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "[/color]

        string s = "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR ";

        if (s.EndsWith(" OR "))
        {
        // First argument is the start index, second argument is the number of characters.
        s = s.Substring(0, s.LastIndexOf(" OR "));
        }

        Debug.Print(s);

        // should print: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "

        --
        Dave Sexton
        dave@www..jwaon line..com
        -----------------------------------------------------------------------
        "huzz" <huzz@discussio ns.microsoft.co m> wrote in message news:BDED1007-FE25-41F7-A5C4-CD5B9927F263@mi crosoft.com...[color=blue]
        > How do i get rid of the "OR " (at the end) from this string?
        >
        > I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
        > I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
        >
        > Many thanks[/color]


        Comment

        • Morten Wennevik

          #5
          Re: question on substring

          Hi huzz,

          See my reply in the forms group.


          --
          Happy Coding!
          Morten Wennevik [C# MVP]

          Comment

          • Gishu

            #6
            RE: question on substring



            "huzz" wrote:
            [color=blue]
            > How do i get rid of the "OR " (at the end) from this string?
            >
            > I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
            > I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
            >
            > Many thanks[/color]

            Use a combination of LastIndexOf("OR ") and Substring methods of the String
            class
            Peek at MSDN to get more details...

            Comment

            • Morten Wennevik

              #7
              Re: question on substring

              Sorry about that, my news reader had a hickup and I thought for a moment I
              saw your question in another group, and that I answered your question
              there.

              Please ignore.

              --
              Happy Coding!
              Morten Wennevik [C# MVP]

              Comment

              • Cor Ligthert

                #8
                Re: question on substring

                huzz,

                As alternative

                String s = "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR ";
                s = s.Substring(0, s.LastIndexOf(" OR"));

                Cor


                Comment

                • RiteshDotNet

                  #9
                  RE: question on substring

                  Best way is u can use string.endtrim( "or")

                  "huzz" wrote:
                  [color=blue]
                  > How do i get rid of the "OR " (at the end) from this string?
                  >
                  > I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
                  > I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
                  >
                  > Many thanks[/color]

                  Comment

                  • Dave

                    #10
                    Re: question on substring

                    Amazing. Of all my time using .NET I've never seen that function. I looked it up.

                    It's called TrimEnd(params char[])

                    So, you can use:

                    s = s.TrimEnd(' ', 'O', 'r', ' ');

                    It's case sensitive. It may not be the best approach to hardcode the character literals. You could also do this:

                    s = s.TrimEnd(" Or ".ToCharArray() );

                    or

                    string toTrim = " Or ";
                    s = s.TrimEnd(toTri m.ToCharArray() );

                    Learn something new every day :)

                    There are mutliple ways of accomplishing the task at hand. The best one depends on your particular situation.
                    Check out the System.Text.Str ingBuilder class for a better way to concatinate strings for dynamic SQL queries.

                    --
                    Dave Sexton
                    dave@www..jwaon line..com
                    -----------------------------------------------------------------------
                    "RiteshDotN et" <RiteshDotNet@d iscussions.micr osoft.com> wrote in message news:13FC4CD9-7FF4-4042-8184-AB74ADBF4848@mi crosoft.com...[color=blue]
                    > Best way is u can use string.endtrim( "or")
                    >
                    > "huzz" wrote:
                    >[color=green]
                    >> How do i get rid of the "OR " (at the end) from this string?
                    >>
                    >> I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
                    >> I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
                    >>
                    >> Many thanks[/color][/color]


                    Comment

                    • Michal Dabrowski

                      #11
                      Re: question on substring

                      RiteshDotNet wrote:[color=blue]
                      > Best way is u can use string.endtrim( "or")
                      >[/color]

                      Well, this may easily lead to unwanted results. Imagine following situation:

                      string s = "blah blah or ro rrroo oorr or ";

                      now, if you do:

                      string x = s.TrimEnd(' ', 'o', 'r', ' ');

                      you will end up with x containing:

                      x == "blah blah".

                      This is because TrimEnd() removes _all_ occurences of the provided chars
                      from the end of the string - regardless of their order.
                      This is not necessarily problem in OP's case, however it is better to be
                      aware of that.
                      [color=blue]
                      > "huzz" wrote:
                      >
                      >[color=green]
                      >>How do i get rid of the "OR " (at the end) from this string?
                      >>
                      >>I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
                      >>I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
                      >>
                      >>Many thanks[/color][/color]

                      Michal Dabrowski

                      Comment

                      • Otis Mukinfus

                        #12
                        Re: question on substring

                        On Wed, 06 Apr 2005 11:57:31 +0200, Michal Dabrowski <spam@jest.be > wrote:

                        if(str.EndsWith ("OR "))
                        {
                        str = str.SubString(0 , str.Length - 3);
                        }

                        [color=blue]
                        >RiteshDotNet wrote:[color=green]
                        >> Best way is u can use string.endtrim( "or")
                        >>[/color]
                        >
                        >Well, this may easily lead to unwanted results. Imagine following situation:
                        >
                        >string s = "blah blah or ro rrroo oorr or ";
                        >
                        >now, if you do:
                        >
                        >string x = s.TrimEnd(' ', 'o', 'r', ' ');
                        >
                        >you will end up with x containing:
                        >
                        >x == "blah blah".
                        >
                        >This is because TrimEnd() removes _all_ occurences of the provided chars
                        >from the end of the string - regardless of their order.
                        >This is not necessarily problem in OP's case, however it is better to be
                        >aware of that.
                        >[color=green]
                        >> "huzz" wrote:
                        >>
                        >>[color=darkred]
                        >>>How do i get rid of the "OR " (at the end) from this string?
                        >>>
                        >>>I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
                        >>>I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
                        >>>
                        >>>Many thanks[/color][/color]
                        >
                        >Michal Dabrowski[/color]

                        Otis Mukinfus

                        Comment

                        • Michal Dabrowski

                          #13
                          Re: question on substring

                          Otis Mukinfus wrote:[color=blue]
                          > On Wed, 06 Apr 2005 11:57:31 +0200, Michal Dabrowski <spam@jest.be > wrote:
                          >
                          > if(str.EndsWith ("OR "))
                          > {
                          > str = str.SubString(0 , str.Length - 3);
                          > }
                          >[/color]

                          That's right - I just wanted to point out, that TrimEnd() approach is
                          rather not appropriate here.
                          [color=blue]
                          >
                          >[color=green]
                          >>RiteshDotNe t wrote:
                          >>[color=darkred]
                          >>>Best way is u can use string.endtrim( "or")
                          >>>[/color]
                          >>
                          >>Well, this may easily lead to unwanted results. Imagine following situation:
                          >>
                          >>string s = "blah blah or ro rrroo oorr or ";
                          >>
                          >>now, if you do:
                          >>
                          >>string x = s.TrimEnd(' ', 'o', 'r', ' ');
                          >>
                          >>you will end up with x containing:
                          >>
                          >>x == "blah blah".
                          >>
                          >>This is because TrimEnd() removes _all_ occurences of the provided chars[/color]
                          >[color=green]
                          >>from the end of the string - regardless of their order.[/color]
                          >[color=green]
                          >>This is not necessarily problem in OP's case, however it is better to be
                          >>aware of that.
                          >>
                          >>[color=darkred]
                          >>>"huzz" wrote:
                          >>>
                          >>>
                          >>>
                          >>>>How do i get rid of the "OR " (at the end) from this string?
                          >>>>
                          >>>>I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
                          >>>>I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
                          >>>>
                          >>>>Many thanks[/color]
                          >>
                          >>Michal Dabrowski[/color]
                          >
                          >
                          > Otis Mukinfus
                          > http://www.otismukinfus.com[/color]

                          Michal Dabrowski

                          Comment

                          Working...