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. Trim away the last part of a string

Trim away the last part of a string

Scheduled Pinned Locked Moved C#
tutorial
7 Posts 4 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.
  • S Offline
    S Offline
    Support123
    wrote on last edited by
    #1

    Hi all, i just want to know how to trim away the last part of a string in a variable... I have the following: string strNum = "0821234567,0721234567,0831234567," string strNum2 = ""; if(strNum.EndsWith(",")) { } "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

    G A 2 Replies Last reply
    0
    • S Support123

      Hi all, i just want to know how to trim away the last part of a string in a variable... I have the following: string strNum = "0821234567,0721234567,0831234567," string strNum2 = ""; if(strNum.EndsWith(",")) { } "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You create a new string from the part of the string that you want to keep: strNum = strNum.SubString(0, strNum.Length - 1);

      --- b { font-weight: normal; }

      S 1 Reply Last reply
      0
      • G Guffa

        You create a new string from the part of the string that you want to keep: strNum = strNum.SubString(0, strNum.Length - 1);

        --- b { font-weight: normal; }

        S Offline
        S Offline
        Support123
        wrote on last edited by
        #3

        Thank you for your time... :)

        "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

        L 1 Reply Last reply
        0
        • S Support123

          Thank you for your time... :)

          "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

          L Offline
          L Offline
          lmoelleb
          wrote on last edited by
          #4

          Hmm, you should consider using "Trim" or "TrimEnd" instead of "Substring" - after all, you want to - well - trim :) It will save you the clumpsy "if" construction as well.

          S 1 Reply Last reply
          0
          • L lmoelleb

            Hmm, you should consider using "Trim" or "TrimEnd" instead of "Substring" - after all, you want to - well - trim :) It will save you the clumpsy "if" construction as well.

            S Offline
            S Offline
            Support123
            wrote on last edited by
            #5

            Thank you, the first one worked. so after i checked if there is a "," at the end of the string it just leaves it out. but i'll use the trim end next time. ;) thanks.

            "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

            1 Reply Last reply
            0
            • S Support123

              Hi all, i just want to know how to trim away the last part of a string in a variable... I have the following: string strNum = "0821234567,0721234567,0831234567," string strNum2 = ""; if(strNum.EndsWith(",")) { } "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

              A Offline
              A Offline
              Arjun Mjolnir Bahree
              wrote on last edited by
              #6

              On another note, something faster, and optimized than substring or trimending (think what shall happen calling substring on a very huge string?) strNum[strNum.Length-1] = string.Empty; There are few scenarios where this will be the best and others where this wont be a better idea.

              Excelsior Arjun Bahree "By The Might of Mjolnir" I Came! I Coded! I Conquered!

              L 1 Reply Last reply
              0
              • A Arjun Mjolnir Bahree

                On another note, something faster, and optimized than substring or trimending (think what shall happen calling substring on a very huge string?) strNum[strNum.Length-1] = string.Empty; There are few scenarios where this will be the best and others where this wont be a better idea.

                Excelsior Arjun Bahree "By The Might of Mjolnir" I Came! I Coded! I Conquered!

                L Offline
                L Offline
                lmoelleb
                wrote on last edited by
                #7

                Two problems with your code: strNum[index] is a char. string.Empty is a string. You can't assign a string to a char. Strings can't be changed. Hence strNum[index] is a read-only indexer. So I do not see how this should work. Using a StringBuilder might help if the strings are repeatedly modified, but for a single trim operation it would not make any sense.

                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