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;
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
add a comment |
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
add a comment |
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
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
apex string
edited 9 hours ago
fehays
asked 9 hours ago
fehaysfehays
4395 silver badges17 bronze badges
4395 silver badges17 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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"
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"
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
add a comment |
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"
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
add a comment |
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"
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"
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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