Why does trim() NOT remove char 160?JSON and escaped double quoteReturning a string as part of an Apex class in an AuraEnabled MethodApex code String to char toCharArrayJSON deserialize into wrapper class errorsTrim/remove trailing zeros from decimal apex:inputfieldVariable does not existWhy does NULL count as 4 characters in Apex?Dynamically get the Map of “DataType” and all “Fields”Why does string.replace fail with char codes?

Can pay be witheld for hours cleaning up after closing time?

Why can't an Airbus A330 dump fuel in an emergency?

Defense against attacks using dictionaries

Can realistic planetary invasion have any meaningful strategy?

Earth rotation discrepancy

How is the list of apps allowed to install another apps populated?

I have a player who yells

Please help me identify the bold slashes between staves

Is "The life is beautiful" incorrect or just very non-idiomatic?

Are there account age or level requirements for obtaining special research?

Science fiction short story where aliens contact a drunk about Earth's impending destruction

Why is my Earth simulation slower than the reality?

If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?

Which household object drew this pattern?

Why does trim() NOT remove char 160?

Notepad++ - How to find multiple values on the same line in any permutation

How to use "Du hast/ Du hattest'?

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

Who was president?

What is the hex versus octal timeline?

Is it safe to remove the bottom chords of a series of garage roof trusses?

Does norwegian.no airline overbook flights?

Fried gnocchi with spinach, bacon, cream sauce in a single pan

Singleton Design Pattern implementation in a not traditional way



Why does trim() NOT remove char 160?


JSON and escaped double quoteReturning a string as part of an Apex class in an AuraEnabled MethodApex code String to char toCharArrayJSON deserialize into wrapper class errorsTrim/remove trailing zeros from decimal apex:inputfieldVariable does not existWhy does NULL count as 4 characters in Apex?Dynamically get the Map of “DataType” and all “Fields”Why does string.replace fail with char codes?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I have a scenario where the label of a PicklistEntry contains a char 160 (non breaking space) at the end of the value. So when I call pe.label.getChars() the array output is the following characters: (104, 101, 108, 108, 111, 160).



If I call trim() then getChars(), I'm expecting the trailing 160 character to be removed. However, it's not. When I use char 32, trim() will remove it correctly.



Additionally, using normalizeSpace() DOES remove the char 160.



So my main question is why doesn't trim() remove this character while normalizeSpace() does?



See code example below.



// get 'hello ' from character array using 160 for space
String hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
System.debug('==>' + hello.trim()); //output ==> 'hello '

// get 'hello ' from character array using 32 for space
hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 32 );
System.debug('==>' + hello.trim()); //output ==> 'hello'

// get 'hello ' from character array using 160 for space and call normalizeSpace()
hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
System.debug('==>' + hello.normalizeSpace()); //output ==> 'hello'


EDIT
Additionally, when calling normalizeSpace() the char 160 is actually converted to char 32. So in order to completely trim the 160 and additional 32, I have to call normalizeSpace().trim()



String hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
String normalized = hello.normalizeSpace();
System.debug('==>' + normalized); //output ==> 'hello'
System.debug('==>' + normalized.getChars()); //output ==> (104, 101, 108, 108, 111, 32)









share|improve this question
































    4















    I have a scenario where the label of a PicklistEntry contains a char 160 (non breaking space) at the end of the value. So when I call pe.label.getChars() the array output is the following characters: (104, 101, 108, 108, 111, 160).



    If I call trim() then getChars(), I'm expecting the trailing 160 character to be removed. However, it's not. When I use char 32, trim() will remove it correctly.



    Additionally, using normalizeSpace() DOES remove the char 160.



    So my main question is why doesn't trim() remove this character while normalizeSpace() does?



    See code example below.



    // get 'hello ' from character array using 160 for space
    String hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
    System.debug('==>' + hello.trim()); //output ==> 'hello '

    // get 'hello ' from character array using 32 for space
    hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 32 );
    System.debug('==>' + hello.trim()); //output ==> 'hello'

    // get 'hello ' from character array using 160 for space and call normalizeSpace()
    hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
    System.debug('==>' + hello.normalizeSpace()); //output ==> 'hello'


    EDIT
    Additionally, when calling normalizeSpace() the char 160 is actually converted to char 32. So in order to completely trim the 160 and additional 32, I have to call normalizeSpace().trim()



    String hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
    String normalized = hello.normalizeSpace();
    System.debug('==>' + normalized); //output ==> 'hello'
    System.debug('==>' + normalized.getChars()); //output ==> (104, 101, 108, 108, 111, 32)









    share|improve this question




























      4












      4








      4








      I have a scenario where the label of a PicklistEntry contains a char 160 (non breaking space) at the end of the value. So when I call pe.label.getChars() the array output is the following characters: (104, 101, 108, 108, 111, 160).



      If I call trim() then getChars(), I'm expecting the trailing 160 character to be removed. However, it's not. When I use char 32, trim() will remove it correctly.



      Additionally, using normalizeSpace() DOES remove the char 160.



      So my main question is why doesn't trim() remove this character while normalizeSpace() does?



      See code example below.



      // get 'hello ' from character array using 160 for space
      String hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
      System.debug('==>' + hello.trim()); //output ==> 'hello '

      // get 'hello ' from character array using 32 for space
      hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 32 );
      System.debug('==>' + hello.trim()); //output ==> 'hello'

      // get 'hello ' from character array using 160 for space and call normalizeSpace()
      hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
      System.debug('==>' + hello.normalizeSpace()); //output ==> 'hello'


      EDIT
      Additionally, when calling normalizeSpace() the char 160 is actually converted to char 32. So in order to completely trim the 160 and additional 32, I have to call normalizeSpace().trim()



      String hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
      String normalized = hello.normalizeSpace();
      System.debug('==>' + normalized); //output ==> 'hello'
      System.debug('==>' + normalized.getChars()); //output ==> (104, 101, 108, 108, 111, 32)









      share|improve this question
















      I have a scenario where the label of a PicklistEntry contains a char 160 (non breaking space) at the end of the value. So when I call pe.label.getChars() the array output is the following characters: (104, 101, 108, 108, 111, 160).



      If I call trim() then getChars(), I'm expecting the trailing 160 character to be removed. However, it's not. When I use char 32, trim() will remove it correctly.



      Additionally, using normalizeSpace() DOES remove the char 160.



      So my main question is why doesn't trim() remove this character while normalizeSpace() does?



      See code example below.



      // get 'hello ' from character array using 160 for space
      String hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
      System.debug('==>' + hello.trim()); //output ==> 'hello '

      // get 'hello ' from character array using 32 for space
      hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 32 );
      System.debug('==>' + hello.trim()); //output ==> 'hello'

      // get 'hello ' from character array using 160 for space and call normalizeSpace()
      hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
      System.debug('==>' + hello.normalizeSpace()); //output ==> 'hello'


      EDIT
      Additionally, when calling normalizeSpace() the char 160 is actually converted to char 32. So in order to completely trim the 160 and additional 32, I have to call normalizeSpace().trim()



      String hello = String.fromCharArray(new Integer[] 104, 101, 108, 108, 111, 160 );
      String normalized = hello.normalizeSpace();
      System.debug('==>' + normalized); //output ==> 'hello'
      System.debug('==>' + normalized.getChars()); //output ==> (104, 101, 108, 108, 111, 32)






      apex string






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 9 hours ago







      fehays

















      asked 9 hours ago









      fehaysfehays

      4395 silver badges17 bronze badges




      4395 silver badges17 bronze badges























          1 Answer
          1






          active

          oldest

          votes


















          3















          The documentation for trim says:




          Leading and trailing ASCII control characters such as tabs and newline characters are also removed. White space and control characters that aren’t at the beginning or end of the sentence aren’t removed.




          Taking this literally, only space (ASCII 32), tab (ASCII 9), line feed (ASCII 10), and carriage return (ASCII 13) would be removed, leaving other whitespace, such as non-breaking space, zero-width space, and so on unaffected. This is probably because trim is a very old method, dating back to the beginning of Apex, while normalizeWhitespace is relatively new.




          As a quick alternative that should do what you want:



          // get ' hello ' from character array using 160 for space
          String hello = String.fromCharArray(new Integer[] 160, 104, 101, 108, 108, 111, 160 );
          System.debug('==>"' + hello.replaceAll('^\pIsWhite_Space+|\pIsWhite_Space+$','')+'"'); //output ==>"hello"





          share|improve this answer






















          • 1





            Makes sense, thank you. Is it worth opening a ticket with support to suggest they include these additional characters in trim? I could see a scenario where you don't want to use normalizeSpace since it removes dupes throughout the entire string.

            – fehays
            8 hours ago






          • 1





            @fehays You could try, but my best educated guess is that they would not log a bug, or if they did, it would become Closed/NoFix. If you need a more robust solution, consider String.replaceAll, which accepts a regular expression to replace. For example: ^\pIsWhite_Space+|\pIsWhite_Space+$. I'll include an example for you in the answer.

            – sfdcfox
            6 hours ago













          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "459"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f274599%2fwhy-does-trim-not-remove-char-160%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3















          The documentation for trim says:




          Leading and trailing ASCII control characters such as tabs and newline characters are also removed. White space and control characters that aren’t at the beginning or end of the sentence aren’t removed.




          Taking this literally, only space (ASCII 32), tab (ASCII 9), line feed (ASCII 10), and carriage return (ASCII 13) would be removed, leaving other whitespace, such as non-breaking space, zero-width space, and so on unaffected. This is probably because trim is a very old method, dating back to the beginning of Apex, while normalizeWhitespace is relatively new.




          As a quick alternative that should do what you want:



          // get ' hello ' from character array using 160 for space
          String hello = String.fromCharArray(new Integer[] 160, 104, 101, 108, 108, 111, 160 );
          System.debug('==>"' + hello.replaceAll('^\pIsWhite_Space+|\pIsWhite_Space+$','')+'"'); //output ==>"hello"





          share|improve this answer






















          • 1





            Makes sense, thank you. Is it worth opening a ticket with support to suggest they include these additional characters in trim? I could see a scenario where you don't want to use normalizeSpace since it removes dupes throughout the entire string.

            – fehays
            8 hours ago






          • 1





            @fehays You could try, but my best educated guess is that they would not log a bug, or if they did, it would become Closed/NoFix. If you need a more robust solution, consider String.replaceAll, which accepts a regular expression to replace. For example: ^\pIsWhite_Space+|\pIsWhite_Space+$. I'll include an example for you in the answer.

            – sfdcfox
            6 hours ago















          3















          The documentation for trim says:




          Leading and trailing ASCII control characters such as tabs and newline characters are also removed. White space and control characters that aren’t at the beginning or end of the sentence aren’t removed.




          Taking this literally, only space (ASCII 32), tab (ASCII 9), line feed (ASCII 10), and carriage return (ASCII 13) would be removed, leaving other whitespace, such as non-breaking space, zero-width space, and so on unaffected. This is probably because trim is a very old method, dating back to the beginning of Apex, while normalizeWhitespace is relatively new.




          As a quick alternative that should do what you want:



          // get ' hello ' from character array using 160 for space
          String hello = String.fromCharArray(new Integer[] 160, 104, 101, 108, 108, 111, 160 );
          System.debug('==>"' + hello.replaceAll('^\pIsWhite_Space+|\pIsWhite_Space+$','')+'"'); //output ==>"hello"





          share|improve this answer






















          • 1





            Makes sense, thank you. Is it worth opening a ticket with support to suggest they include these additional characters in trim? I could see a scenario where you don't want to use normalizeSpace since it removes dupes throughout the entire string.

            – fehays
            8 hours ago






          • 1





            @fehays You could try, but my best educated guess is that they would not log a bug, or if they did, it would become Closed/NoFix. If you need a more robust solution, consider String.replaceAll, which accepts a regular expression to replace. For example: ^\pIsWhite_Space+|\pIsWhite_Space+$. I'll include an example for you in the answer.

            – sfdcfox
            6 hours ago













          3














          3










          3









          The documentation for trim says:




          Leading and trailing ASCII control characters such as tabs and newline characters are also removed. White space and control characters that aren’t at the beginning or end of the sentence aren’t removed.




          Taking this literally, only space (ASCII 32), tab (ASCII 9), line feed (ASCII 10), and carriage return (ASCII 13) would be removed, leaving other whitespace, such as non-breaking space, zero-width space, and so on unaffected. This is probably because trim is a very old method, dating back to the beginning of Apex, while normalizeWhitespace is relatively new.




          As a quick alternative that should do what you want:



          // get ' hello ' from character array using 160 for space
          String hello = String.fromCharArray(new Integer[] 160, 104, 101, 108, 108, 111, 160 );
          System.debug('==>"' + hello.replaceAll('^\pIsWhite_Space+|\pIsWhite_Space+$','')+'"'); //output ==>"hello"





          share|improve this answer















          The documentation for trim says:




          Leading and trailing ASCII control characters such as tabs and newline characters are also removed. White space and control characters that aren’t at the beginning or end of the sentence aren’t removed.




          Taking this literally, only space (ASCII 32), tab (ASCII 9), line feed (ASCII 10), and carriage return (ASCII 13) would be removed, leaving other whitespace, such as non-breaking space, zero-width space, and so on unaffected. This is probably because trim is a very old method, dating back to the beginning of Apex, while normalizeWhitespace is relatively new.




          As a quick alternative that should do what you want:



          // get ' hello ' from character array using 160 for space
          String hello = String.fromCharArray(new Integer[] 160, 104, 101, 108, 108, 111, 160 );
          System.debug('==>"' + hello.replaceAll('^\pIsWhite_Space+|\pIsWhite_Space+$','')+'"'); //output ==>"hello"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 6 hours ago

























          answered 8 hours ago









          sfdcfoxsfdcfox

          283k14 gold badges231 silver badges484 bronze badges




          283k14 gold badges231 silver badges484 bronze badges










          • 1





            Makes sense, thank you. Is it worth opening a ticket with support to suggest they include these additional characters in trim? I could see a scenario where you don't want to use normalizeSpace since it removes dupes throughout the entire string.

            – fehays
            8 hours ago






          • 1





            @fehays You could try, but my best educated guess is that they would not log a bug, or if they did, it would become Closed/NoFix. If you need a more robust solution, consider String.replaceAll, which accepts a regular expression to replace. For example: ^\pIsWhite_Space+|\pIsWhite_Space+$. I'll include an example for you in the answer.

            – sfdcfox
            6 hours ago












          • 1





            Makes sense, thank you. Is it worth opening a ticket with support to suggest they include these additional characters in trim? I could see a scenario where you don't want to use normalizeSpace since it removes dupes throughout the entire string.

            – fehays
            8 hours ago






          • 1





            @fehays You could try, but my best educated guess is that they would not log a bug, or if they did, it would become Closed/NoFix. If you need a more robust solution, consider String.replaceAll, which accepts a regular expression to replace. For example: ^\pIsWhite_Space+|\pIsWhite_Space+$. I'll include an example for you in the answer.

            – sfdcfox
            6 hours ago







          1




          1





          Makes sense, thank you. Is it worth opening a ticket with support to suggest they include these additional characters in trim? I could see a scenario where you don't want to use normalizeSpace since it removes dupes throughout the entire string.

          – fehays
          8 hours ago





          Makes sense, thank you. Is it worth opening a ticket with support to suggest they include these additional characters in trim? I could see a scenario where you don't want to use normalizeSpace since it removes dupes throughout the entire string.

          – fehays
          8 hours ago




          1




          1





          @fehays You could try, but my best educated guess is that they would not log a bug, or if they did, it would become Closed/NoFix. If you need a more robust solution, consider String.replaceAll, which accepts a regular expression to replace. For example: ^\pIsWhite_Space+|\pIsWhite_Space+$. I'll include an example for you in the answer.

          – sfdcfox
          6 hours ago





          @fehays You could try, but my best educated guess is that they would not log a bug, or if they did, it would become Closed/NoFix. If you need a more robust solution, consider String.replaceAll, which accepts a regular expression to replace. For example: ^\pIsWhite_Space+|\pIsWhite_Space+$. I'll include an example for you in the answer.

          – sfdcfox
          6 hours ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f274599%2fwhy-does-trim-not-remove-char-160%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

          199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單