Bug? String.split chopping off empty trailing valueBug in String.split('.')?PushTopic Test: Duplicate Name Bug?Spring'14 (bug?) with apex:repeat and setsRecentlyViewed objects not returning Email… Sf bug?apex:outputField not applying styleClass — bug?add trailing values to the stringLightning experience bug with empty (custom) object's list to orderHow to check empty value in a string?Unknown error in Salesforce Production Org. Is this a bug in Salesforce?How to set string query using not equal to empty?
How can bays and straits be determined in a procedurally generated map?
Was any UN Security Council vote triple-vetoed?
NMaximize is not converging to a solution
Is it possible to do 50 km distance without any previous training?
Roll the carpet
Why doesn't H₄O²⁺ exist?
What's the output of a record needle playing an out-of-speed record
Are the number of citations and number of published articles the most important criteria for a tenure promotion?
What would happen to a modern skyscraper if it rains micro blackholes?
Why can't I see bouncing of switch on oscilloscope screen?
meaning of に in 本当に?
Do I have a twin with permutated remainders?
Horror movie about a virus at the prom; beginning and end are stylized as a cartoon
What defenses are there against being summoned by the Gate spell?
Is it inappropriate for a student to attend their mentor's dissertation defense?
Unable to deploy metadata from Partner Developer scratch org because of extra fields
dbcc cleantable batch size explanation
Watching something be written to a file live with tail
Why is Minecraft giving an OpenGL error?
What is the word for reserving something for yourself before others do?
What does "Puller Prush Person" mean?
Could an aircraft fly or hover using only jets of compressed air?
Modeling an IP Address
How does quantile regression compare to logistic regression with the variable split at the quantile?
Bug? String.split chopping off empty trailing value
Bug in String.split('.')?PushTopic Test: Duplicate Name Bug?Spring'14 (bug?) with apex:repeat and setsRecentlyViewed objects not returning Email… Sf bug?apex:outputField not applying styleClass — bug?add trailing values to the stringLightning experience bug with empty (custom) object's list to orderHow to check empty value in a string?Unknown error in Salesforce Production Org. Is this a bug in Salesforce?How to set string query using not equal to empty?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have found this strange behavior by trying to write a unit test against a CSV Generator. It seems that if I try to split a string by comma, and there is an empty trailing cell, these values are ignored. Oddly, leading empty cells are fine. Is this behavior a bug? Should I be using a different expression than ,? Is there a version where it works as I expect?
Script
Integer repro(String commaSeparated)
return commaSeparated.split(',').size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
Log
USER_DEBUG [5]|DEBUG|Should be 2: 1
USER_DEBUG [7]|DEBUG|Should be 3: 3
apex string bug
add a comment |
I have found this strange behavior by trying to write a unit test against a CSV Generator. It seems that if I try to split a string by comma, and there is an empty trailing cell, these values are ignored. Oddly, leading empty cells are fine. Is this behavior a bug? Should I be using a different expression than ,? Is there a version where it works as I expect?
Script
Integer repro(String commaSeparated)
return commaSeparated.split(',').size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
Log
USER_DEBUG [5]|DEBUG|Should be 2: 1
USER_DEBUG [7]|DEBUG|Should be 3: 3
apex string bug
add a comment |
I have found this strange behavior by trying to write a unit test against a CSV Generator. It seems that if I try to split a string by comma, and there is an empty trailing cell, these values are ignored. Oddly, leading empty cells are fine. Is this behavior a bug? Should I be using a different expression than ,? Is there a version where it works as I expect?
Script
Integer repro(String commaSeparated)
return commaSeparated.split(',').size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
Log
USER_DEBUG [5]|DEBUG|Should be 2: 1
USER_DEBUG [7]|DEBUG|Should be 3: 3
apex string bug
I have found this strange behavior by trying to write a unit test against a CSV Generator. It seems that if I try to split a string by comma, and there is an empty trailing cell, these values are ignored. Oddly, leading empty cells are fine. Is this behavior a bug? Should I be using a different expression than ,? Is there a version where it works as I expect?
Script
Integer repro(String commaSeparated)
return commaSeparated.split(',').size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
Log
USER_DEBUG [5]|DEBUG|Should be 2: 1
USER_DEBUG [7]|DEBUG|Should be 3: 3
apex string bug
apex string bug
asked 2 days ago
Adrian Larson♦Adrian Larson
109k19119251
109k19119251
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
According to the documentation String Class, here you need to pass the limit which is optional one.
The optional limit parameter controls the number of times the pattern is applied and therefore affects the length of the list.
If limit is greater than zero:
- The pattern is applied a maximum of (limit – 1) times.
- The list’s length is no greater than limit.
- The list’s last entry contains all input beyond the last matched delimiter.
If limit is non-positive, the pattern is applied as many times as possible, and the list can have any length.
If limit is zero, the pattern is applied as many times as possible, the list can have any length, and trailing empty strings are discarded.
So, passing -1 as limit in this split(regExp, limit) method gives me desired results.
Integer repro(String commaSeparated)
return commaSeparated.split(',',-1).size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
N.B. this behavior is version dependent, if I recall correctly. Previously, it worked like it did in Java 7, but was then changed to work like it does in Java 8. I don't recall where the version change happened, probably 28.0 or 32.0, but definitely worth paying attention to release notes and testing on the selected API version.
– sfdcfox
2 days ago
I have checked that at current version 45.0, it is perfectly working
– Santanu Boral
2 days ago
1
Yes, but not all classes in an org might be at the same version. It's simply a word of caution for those that depend on this answer without checking the API version.
– sfdcfox
2 days 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%2f256297%2fbug-string-split-chopping-off-empty-trailing-value%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
According to the documentation String Class, here you need to pass the limit which is optional one.
The optional limit parameter controls the number of times the pattern is applied and therefore affects the length of the list.
If limit is greater than zero:
- The pattern is applied a maximum of (limit – 1) times.
- The list’s length is no greater than limit.
- The list’s last entry contains all input beyond the last matched delimiter.
If limit is non-positive, the pattern is applied as many times as possible, and the list can have any length.
If limit is zero, the pattern is applied as many times as possible, the list can have any length, and trailing empty strings are discarded.
So, passing -1 as limit in this split(regExp, limit) method gives me desired results.
Integer repro(String commaSeparated)
return commaSeparated.split(',',-1).size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
N.B. this behavior is version dependent, if I recall correctly. Previously, it worked like it did in Java 7, but was then changed to work like it does in Java 8. I don't recall where the version change happened, probably 28.0 or 32.0, but definitely worth paying attention to release notes and testing on the selected API version.
– sfdcfox
2 days ago
I have checked that at current version 45.0, it is perfectly working
– Santanu Boral
2 days ago
1
Yes, but not all classes in an org might be at the same version. It's simply a word of caution for those that depend on this answer without checking the API version.
– sfdcfox
2 days ago
add a comment |
According to the documentation String Class, here you need to pass the limit which is optional one.
The optional limit parameter controls the number of times the pattern is applied and therefore affects the length of the list.
If limit is greater than zero:
- The pattern is applied a maximum of (limit – 1) times.
- The list’s length is no greater than limit.
- The list’s last entry contains all input beyond the last matched delimiter.
If limit is non-positive, the pattern is applied as many times as possible, and the list can have any length.
If limit is zero, the pattern is applied as many times as possible, the list can have any length, and trailing empty strings are discarded.
So, passing -1 as limit in this split(regExp, limit) method gives me desired results.
Integer repro(String commaSeparated)
return commaSeparated.split(',',-1).size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
N.B. this behavior is version dependent, if I recall correctly. Previously, it worked like it did in Java 7, but was then changed to work like it does in Java 8. I don't recall where the version change happened, probably 28.0 or 32.0, but definitely worth paying attention to release notes and testing on the selected API version.
– sfdcfox
2 days ago
I have checked that at current version 45.0, it is perfectly working
– Santanu Boral
2 days ago
1
Yes, but not all classes in an org might be at the same version. It's simply a word of caution for those that depend on this answer without checking the API version.
– sfdcfox
2 days ago
add a comment |
According to the documentation String Class, here you need to pass the limit which is optional one.
The optional limit parameter controls the number of times the pattern is applied and therefore affects the length of the list.
If limit is greater than zero:
- The pattern is applied a maximum of (limit – 1) times.
- The list’s length is no greater than limit.
- The list’s last entry contains all input beyond the last matched delimiter.
If limit is non-positive, the pattern is applied as many times as possible, and the list can have any length.
If limit is zero, the pattern is applied as many times as possible, the list can have any length, and trailing empty strings are discarded.
So, passing -1 as limit in this split(regExp, limit) method gives me desired results.
Integer repro(String commaSeparated)
return commaSeparated.split(',',-1).size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
According to the documentation String Class, here you need to pass the limit which is optional one.
The optional limit parameter controls the number of times the pattern is applied and therefore affects the length of the list.
If limit is greater than zero:
- The pattern is applied a maximum of (limit – 1) times.
- The list’s length is no greater than limit.
- The list’s last entry contains all input beyond the last matched delimiter.
If limit is non-positive, the pattern is applied as many times as possible, and the list can have any length.
If limit is zero, the pattern is applied as many times as possible, the list can have any length, and trailing empty strings are discarded.
So, passing -1 as limit in this split(regExp, limit) method gives me desired results.
Integer repro(String commaSeparated)
return commaSeparated.split(',',-1).size();
system.debug('Should be 2: ' + repro('2,'));
system.debug('Should be 3: ' + repro(',,3'));
answered 2 days ago
Santanu BoralSantanu Boral
31.2k52356
31.2k52356
N.B. this behavior is version dependent, if I recall correctly. Previously, it worked like it did in Java 7, but was then changed to work like it does in Java 8. I don't recall where the version change happened, probably 28.0 or 32.0, but definitely worth paying attention to release notes and testing on the selected API version.
– sfdcfox
2 days ago
I have checked that at current version 45.0, it is perfectly working
– Santanu Boral
2 days ago
1
Yes, but not all classes in an org might be at the same version. It's simply a word of caution for those that depend on this answer without checking the API version.
– sfdcfox
2 days ago
add a comment |
N.B. this behavior is version dependent, if I recall correctly. Previously, it worked like it did in Java 7, but was then changed to work like it does in Java 8. I don't recall where the version change happened, probably 28.0 or 32.0, but definitely worth paying attention to release notes and testing on the selected API version.
– sfdcfox
2 days ago
I have checked that at current version 45.0, it is perfectly working
– Santanu Boral
2 days ago
1
Yes, but not all classes in an org might be at the same version. It's simply a word of caution for those that depend on this answer without checking the API version.
– sfdcfox
2 days ago
N.B. this behavior is version dependent, if I recall correctly. Previously, it worked like it did in Java 7, but was then changed to work like it does in Java 8. I don't recall where the version change happened, probably 28.0 or 32.0, but definitely worth paying attention to release notes and testing on the selected API version.
– sfdcfox
2 days ago
N.B. this behavior is version dependent, if I recall correctly. Previously, it worked like it did in Java 7, but was then changed to work like it does in Java 8. I don't recall where the version change happened, probably 28.0 or 32.0, but definitely worth paying attention to release notes and testing on the selected API version.
– sfdcfox
2 days ago
I have checked that at current version 45.0, it is perfectly working
– Santanu Boral
2 days ago
I have checked that at current version 45.0, it is perfectly working
– Santanu Boral
2 days ago
1
1
Yes, but not all classes in an org might be at the same version. It's simply a word of caution for those that depend on this answer without checking the API version.
– sfdcfox
2 days ago
Yes, but not all classes in an org might be at the same version. It's simply a word of caution for those that depend on this answer without checking the API version.
– sfdcfox
2 days 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%2f256297%2fbug-string-split-chopping-off-empty-trailing-value%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