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. does Trim() work??

does Trim() work??

Scheduled Pinned Locked Moved C#
data-structuresquestion
8 Posts 5 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
    Senkwe Chanda
    wrote on last edited by
    #1

    When I run this,

    using System;

    namespace trim
    {
    public class trim
    {
    static void Main()
    {
    string str = "Senkwe ";
    Console.WriteLine(str.Length);
    str.Trim();
    Console.WriteLine(str.Length);
    }
    }
    }

    I get an output of 10 each time. Am I using trim improperly? I even tried specifying an array of characters to be reomoved ie char[] myChars = {' '}; and it still didn't work. Any ideas? Regards Senkwe Just another wannabe code junky

    R 1 Reply Last reply
    0
    • S Senkwe Chanda

      When I run this,

      using System;

      namespace trim
      {
      public class trim
      {
      static void Main()
      {
      string str = "Senkwe ";
      Console.WriteLine(str.Length);
      str.Trim();
      Console.WriteLine(str.Length);
      }
      }
      }

      I get an output of 10 each time. Am I using trim improperly? I even tried specifying an array of characters to be reomoved ie char[] myChars = {' '}; and it still didn't work. Any ideas? Regards Senkwe Just another wannabe code junky

      R Offline
      R Offline
      Rupel
      wrote on last edited by
      #2

      Trim returns the new string!

      str=str.Trim();

      should be the solution i got into this trap with the Registry-class: when you want to open a subkey you would write

      key.open(location);

      but instead the handle to the opened key is returned. so

      key=key.open(location);

      worked. in general: someone has to explain to me, what the idea behind this weired design is? this behaviour of returning the result looks to me like a static function (like Color.FromArgb()) - which Trim and this registry-open-function are not. i don't like that design - and obviously i'm not the only one, that was caught in that trap... :wq

      S 1 Reply Last reply
      0
      • R Rupel

        Trim returns the new string!

        str=str.Trim();

        should be the solution i got into this trap with the Registry-class: when you want to open a subkey you would write

        key.open(location);

        but instead the handle to the opened key is returned. so

        key=key.open(location);

        worked. in general: someone has to explain to me, what the idea behind this weired design is? this behaviour of returning the result looks to me like a static function (like Color.FromArgb()) - which Trim and this registry-open-function are not. i don't like that design - and obviously i'm not the only one, that was caught in that trap... :wq

        S Offline
        S Offline
        Senkwe Chanda
        wrote on last edited by
        #3

        Thanks alot! Boy, that was so un-obvious to me. To make matters worse, the documentation had no real examples of how to use it. I agree that it is wierd and inconsistent. Thanks again Regards Senkwe Just another wannabe code junky

        E 1 Reply Last reply
        0
        • S Senkwe Chanda

          Thanks alot! Boy, that was so un-obvious to me. To make matters worse, the documentation had no real examples of how to use it. I agree that it is wierd and inconsistent. Thanks again Regards Senkwe Just another wannabe code junky

          E Offline
          E Offline
          ez2
          wrote on last edited by
          #4

          My guess as to why it works this way is that strings are immutable (can't be changed). So methods that apprear to change the string actually return a modified copy. The original string stays intact which is why you needed to assign the returned modified string to the orginal string variable (i.e. str = str.Trim();

          J 1 Reply Last reply
          0
          • E ez2

            My guess as to why it works this way is that strings are immutable (can't be changed). So methods that apprear to change the string actually return a modified copy. The original string stays intact which is why you needed to assign the returned modified string to the orginal string variable (i.e. str = str.Trim();

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            Exactly :) James Simplicity Rules!

            C 1 Reply Last reply
            0
            • J James T Johnson

              Exactly :) James Simplicity Rules!

              C Offline
              C Offline
              ColinDavies
              wrote on last edited by
              #6

              You really understand this stuff well ! Congrats Regardz Colin J Davies

              Sonork ID 100.9197:Colin

              Free Colin Davies

              "real Americans don't criticize their leaders - because they don't want the terrorists to win." -- Quote from Chris Losinger a real American"

              J 1 Reply Last reply
              0
              • C ColinDavies

                You really understand this stuff well ! Congrats Regardz Colin J Davies

                Sonork ID 100.9197:Colin

                Free Colin Davies

                "real Americans don't criticize their leaders - because they don't want the terrorists to win." -- Quote from Chris Losinger a real American"

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                ****Colin Davies wrote: You really understand this stuff well ! Congrats Thanks :) I try to be really good at what I do, currently that is C# and .NET :-D James Simplicity Rules!

                C 1 Reply Last reply
                0
                • J James T Johnson

                  ****Colin Davies wrote: You really understand this stuff well ! Congrats Thanks :) I try to be really good at what I do, currently that is C# and .NET :-D James Simplicity Rules!

                  C Offline
                  C Offline
                  ColinDavies
                  wrote on last edited by
                  #8

                  James T. Johnson wrote: I try to be really good at what I do, currently that is C# and .NET Thats a good attitude to have ! I hope you succeede ? Regardz Colin J Davies

                  Sonork ID 100.9197:Colin

                  Free Colin Davies

                  "real Americans don't criticize their leaders - because they don't want the terrorists to win." -- Quote from Chris Losinger a real American"

                  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