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?

What does “the session was packed” mean in this context?

How do I know where to place holes on an instrument?

Is it logically or scientifically possible to artificially send energy to the body?

What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?

Extract rows of a table, that include less than x NULLs

How to show a landlord what we have in savings?

Forgetting the musical notes while performing in concert

Avoiding direct proof while writing proof by induction

Solving a recurrence relation (poker chips)

Why no variance term in Bayesian logistic regression?

iPad being using in wall mount battery swollen

How much of data wrangling is a data scientist's job?

How can I determine if the org that I'm currently connected to is a scratch org?

Can a virus destroy the BIOS of a modern computer?

Assassin's bullet with mercury

Do UK voters know if their MP will be the Speaker of the House?

Is the myth that if you can play one instrument, you can learn another instrument with ease true?

Intersection Puzzle

How does a predictive coding aid in lossless compression?

Im going to France and my passport expires June 19th

A friend helped me with a presentation – plagiarism or not?

How seriously should I take size and weight limits of hand luggage?

What killed these X2 caps?

Can we compute the area of a quadrilateral with one right angle when we only know the lengths of any three sides?



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?













3















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











share|improve this question


























    3















    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











    share|improve this question
























      3












      3








      3








      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











      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Adrian LarsonAdrian Larson

      109k19118251




      109k19118251




















          1 Answer
          1






          active

          oldest

          votes


















          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'));





          share|improve this answer























          • 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
            yesterday











          • I have checked that at current version 45.0, it is perfectly working

            – Santanu Boral
            23 hours 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
            22 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%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









          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'));





          share|improve this answer























          • 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
            yesterday











          • I have checked that at current version 45.0, it is perfectly working

            – Santanu Boral
            23 hours 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
            22 hours ago















          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'));





          share|improve this answer























          • 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
            yesterday











          • I have checked that at current version 45.0, it is perfectly working

            – Santanu Boral
            23 hours 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
            22 hours ago













          3












          3








          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'));





          share|improve this answer













          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'));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          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
            yesterday











          • I have checked that at current version 45.0, it is perfectly working

            – Santanu Boral
            23 hours 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
            22 hours 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
            yesterday











          • I have checked that at current version 45.0, it is perfectly working

            – Santanu Boral
            23 hours 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
            22 hours 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
          yesterday





          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
          yesterday













          I have checked that at current version 45.0, it is perfectly working

          – Santanu Boral
          23 hours ago





          I have checked that at current version 45.0, it is perfectly working

          – Santanu Boral
          23 hours 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
          22 hours 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
          22 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%2f256297%2fbug-string-split-chopping-off-empty-trailing-value%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

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form