Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# string operation

c# string operation

Scheduled Pinned Locked Moved C#
csharpquestion
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AndieDu

    This is not a homework, this is a question that i got during my previous interview :(

    A Offline
    A Offline
    Abhinav S
    wrote on last edited by
    #5

    Without using trim - locate the last space in a string and then use substring to get the rest of the string.

    There are only 10 types of people in this world — those who understand binary, and those who don't.

    OriginalGriffO 1 Reply Last reply
    0
    • A Abhinav S

      Without using trim - locate the last space in a string and then use substring to get the rest of the string.

      There are only 10 types of people in this world — those who understand binary, and those who don't.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #6

      Abhinav S wrote:

      Without using trim - locate the last space in a string and then use substring to get the rest of the string. And repeat...

      Otherwise it just removes the final space, unlike TrimEnd which removes them all. Except it also removes trailing tab characters. A better solution might be:

      Regex.Replace(s, @"[\s]*$", "")

      Which does the lot for you.

      All those who believe in psycho kinesis, raise my hand.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • A AndieDu

        Hi All, say i have these inputs strings: string input_1 = " ABCD "; string input_2 = " A BC D "; string input_3 " A B C D "; I want to convert these strings to the follows: input_1 = " ABCD"; input_2 = " A BC D"; input_3 " A B C D"; basically remove all the blanks after the last letter, i know the string.TrimEnd() function in .Net can be used to achieve my requirement. Is there another way (without using string.TrimEnd() function) to achieve the same as String.TrimEnd() does? Many thanks

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #7

        The simplest solution is to use a regular expression:

        Regex.Replace(inputString, @"[\s]*$", "")

        All those who believe in psycho kinesis, raise my hand.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        A 1 Reply Last reply
        0
        • A AndieDu

          Hi All, say i have these inputs strings: string input_1 = " ABCD "; string input_2 = " A BC D "; string input_3 " A B C D "; I want to convert these strings to the follows: input_1 = " ABCD"; input_2 = " A BC D"; input_3 " A B C D"; basically remove all the blanks after the last letter, i know the string.TrimEnd() function in .Net can be used to achieve my requirement. Is there another way (without using string.TrimEnd() function) to achieve the same as String.TrimEnd() does? Many thanks

          T Offline
          T Offline
          theOzLizard
          wrote on last edited by
          #8

          you can do input_3 = inputstr.Substring(0, inputstr.LastIndexOf(" ")); will give you the result you want.

          theLizard

          OriginalGriffO 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            The simplest solution is to use a regular expression:

            Regex.Replace(inputString, @"[\s]*$", "")

            All those who believe in psycho kinesis, raise my hand.

            A Offline
            A Offline
            AndieDu
            wrote on last edited by
            #9

            this is the most efficient and accruate way, thanks very much.

            1 Reply Last reply
            0
            • T theOzLizard

              you can do input_3 = inputstr.Substring(0, inputstr.LastIndexOf(" ")); will give you the result you want.

              theLizard

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #10

              Unfortunately, that turns out not to be the case. It will remove all data from the last occurance of a space to the end: "A<space>B<space>C<space><space>" becomes "A<space>B<space>C<space>" "A<space>B<space>C" becomes "A<space>B"

              All those who believe in psycho kinesis, raise my hand.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              T 1 Reply Last reply
              0
              • A AndieDu

                Hi All, say i have these inputs strings: string input_1 = " ABCD "; string input_2 = " A BC D "; string input_3 " A B C D "; I want to convert these strings to the follows: input_1 = " ABCD"; input_2 = " A BC D"; input_3 " A B C D"; basically remove all the blanks after the last letter, i know the string.TrimEnd() function in .Net can be used to achieve my requirement. Is there another way (without using string.TrimEnd() function) to achieve the same as String.TrimEnd() does? Many thanks

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #11

                There are plenty of ways of doing this. Here is one: You can make use of LastIndexOf and Replace method along with the Length property. Check the last index of the space and if it is equal to the length of the string, replace it with nothing. You can also use the Remove method instead of Replace.

                50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                OriginalGriffO 1 Reply Last reply
                0
                • D dan sh

                  There are plenty of ways of doing this. Here is one: You can make use of LastIndexOf and Replace method along with the Length property. Check the last index of the space and if it is equal to the length of the string, replace it with nothing. You can also use the Remove method instead of Replace.

                  50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #12

                  Doesn't achieve what he asked for: it only removes the last space, not all trailing spaces.

                  All those who believe in psycho kinesis, raise my hand.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  D 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Doesn't achieve what he asked for: it only removes the last space, not all trailing spaces.

                    All those who believe in psycho kinesis, raise my hand.

                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #13

                    Do that recursively. :) /This approach can win the worst approach award. //No, I can beat this one too.

                    50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                    OriginalGriffO 1 Reply Last reply
                    0
                    • D dan sh

                      Do that recursively. :) /This approach can win the worst approach award. //No, I can beat this one too.

                      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #14

                      Mmmm! I love the smell of sledgehammer in the morning! :laugh:

                      All those who believe in psycho kinesis, raise my hand.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      D 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        Mmmm! I love the smell of sledgehammer in the morning! :laugh:

                        All those who believe in psycho kinesis, raise my hand.

                        D Offline
                        D Offline
                        dan sh
                        wrote on last edited by
                        #15

                        Just the smell. Blow - never. :)

                        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Unfortunately, that turns out not to be the case. It will remove all data from the last occurance of a space to the end: "A<space>B<space>C<space><space>" becomes "A<space>B<space>C<space>" "A<space>B<space>C" becomes "A<space>B"

                          All those who believe in psycho kinesis, raise my hand.

                          T Offline
                          T Offline
                          theOzLizard
                          wrote on last edited by
                          #16

                          Oops, less haste, If I could not use trim() it should have been something like if(string.LastIndexOf(" ") = string.Length) { string = string.Substring(0, string.Length-1); } But the answer from OriginalGriff seems to be the one, I have also learned.

                          theLizard

                          P 1 Reply Last reply
                          0
                          • T theOzLizard

                            Oops, less haste, If I could not use trim() it should have been something like if(string.LastIndexOf(" ") = string.Length) { string = string.Substring(0, string.Length-1); } But the answer from OriginalGriff seems to be the one, I have also learned.

                            theLizard

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #17

                            Make it a while loop and use a comparison operator rather than assignment. :-D

                            T 1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              Make it a while loop and use a comparison operator rather than assignment. :-D

                              T Offline
                              T Offline
                              theOzLizard
                              wrote on last edited by
                              #18

                              oops (again, bugger) :( left out one of these = and yes a while would be appropriate if more than one at end of string. I'm having an off day

                              theLizard

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups