Get everything in string after a single comma [duplicate]










0
















This question already has an answer here:



  • How do I split a string with multiple separators in javascript?

    16 answers



  • Convert comma separated string to array

    11 answers



I have some problem with my string, the variable name is accountcode. I want only part of the string. I want everything in the string which is after the first ,, excluding any extra space after the comma. For example:



accountcode = "xxxx, tes";



accountcode = "xxxx, hello";



Then I want to output like tes and hello.



I tried:



var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);









share|improve this question















marked as duplicate by Temani Afif, Makyen, Community Nov 16 '18 at 1:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • So, just to clarify, accountcode is an string with multiple words separated by commas?

    – Shidersz
    Nov 15 '18 at 4:11






  • 1





    @Mukyuu This is a javascript thread

    – Abana Clara
    Nov 15 '18 at 4:17






  • 1





    Why is this marked as a duplicate? This is not Java.

    – Spencer Wieczorek
    Nov 15 '18 at 4:19







  • 1





    I'm all for marking duplicates. But this one misses by a 6'2" ballerina split

    – Abana Clara
    Nov 15 '18 at 4:22






  • 2





    Weird duplicate vote... Either pick this or this as dupe.

    – KarelG
    Nov 15 '18 at 7:54
















0
















This question already has an answer here:



  • How do I split a string with multiple separators in javascript?

    16 answers



  • Convert comma separated string to array

    11 answers



I have some problem with my string, the variable name is accountcode. I want only part of the string. I want everything in the string which is after the first ,, excluding any extra space after the comma. For example:



accountcode = "xxxx, tes";



accountcode = "xxxx, hello";



Then I want to output like tes and hello.



I tried:



var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);









share|improve this question















marked as duplicate by Temani Afif, Makyen, Community Nov 16 '18 at 1:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • So, just to clarify, accountcode is an string with multiple words separated by commas?

    – Shidersz
    Nov 15 '18 at 4:11






  • 1





    @Mukyuu This is a javascript thread

    – Abana Clara
    Nov 15 '18 at 4:17






  • 1





    Why is this marked as a duplicate? This is not Java.

    – Spencer Wieczorek
    Nov 15 '18 at 4:19







  • 1





    I'm all for marking duplicates. But this one misses by a 6'2" ballerina split

    – Abana Clara
    Nov 15 '18 at 4:22






  • 2





    Weird duplicate vote... Either pick this or this as dupe.

    – KarelG
    Nov 15 '18 at 7:54














0












0








0









This question already has an answer here:



  • How do I split a string with multiple separators in javascript?

    16 answers



  • Convert comma separated string to array

    11 answers



I have some problem with my string, the variable name is accountcode. I want only part of the string. I want everything in the string which is after the first ,, excluding any extra space after the comma. For example:



accountcode = "xxxx, tes";



accountcode = "xxxx, hello";



Then I want to output like tes and hello.



I tried:



var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);









share|improve this question

















This question already has an answer here:



  • How do I split a string with multiple separators in javascript?

    16 answers



  • Convert comma separated string to array

    11 answers



I have some problem with my string, the variable name is accountcode. I want only part of the string. I want everything in the string which is after the first ,, excluding any extra space after the comma. For example:



accountcode = "xxxx, tes";



accountcode = "xxxx, hello";



Then I want to output like tes and hello.



I tried:



var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);




This question already has an answer here:



  • How do I split a string with multiple separators in javascript?

    16 answers



  • Convert comma separated string to array

    11 answers







javascript jquery string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:48









Makyen

20.7k84074




20.7k84074










asked Nov 15 '18 at 4:08









senaasenaa

3516




3516




marked as duplicate by Temani Afif, Makyen, Community Nov 16 '18 at 1:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Temani Afif, Makyen, Community Nov 16 '18 at 1:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • So, just to clarify, accountcode is an string with multiple words separated by commas?

    – Shidersz
    Nov 15 '18 at 4:11






  • 1





    @Mukyuu This is a javascript thread

    – Abana Clara
    Nov 15 '18 at 4:17






  • 1





    Why is this marked as a duplicate? This is not Java.

    – Spencer Wieczorek
    Nov 15 '18 at 4:19







  • 1





    I'm all for marking duplicates. But this one misses by a 6'2" ballerina split

    – Abana Clara
    Nov 15 '18 at 4:22






  • 2





    Weird duplicate vote... Either pick this or this as dupe.

    – KarelG
    Nov 15 '18 at 7:54


















  • So, just to clarify, accountcode is an string with multiple words separated by commas?

    – Shidersz
    Nov 15 '18 at 4:11






  • 1





    @Mukyuu This is a javascript thread

    – Abana Clara
    Nov 15 '18 at 4:17






  • 1





    Why is this marked as a duplicate? This is not Java.

    – Spencer Wieczorek
    Nov 15 '18 at 4:19







  • 1





    I'm all for marking duplicates. But this one misses by a 6'2" ballerina split

    – Abana Clara
    Nov 15 '18 at 4:22






  • 2





    Weird duplicate vote... Either pick this or this as dupe.

    – KarelG
    Nov 15 '18 at 7:54

















So, just to clarify, accountcode is an string with multiple words separated by commas?

– Shidersz
Nov 15 '18 at 4:11





So, just to clarify, accountcode is an string with multiple words separated by commas?

– Shidersz
Nov 15 '18 at 4:11




1




1





@Mukyuu This is a javascript thread

– Abana Clara
Nov 15 '18 at 4:17





@Mukyuu This is a javascript thread

– Abana Clara
Nov 15 '18 at 4:17




1




1





Why is this marked as a duplicate? This is not Java.

– Spencer Wieczorek
Nov 15 '18 at 4:19






Why is this marked as a duplicate? This is not Java.

– Spencer Wieczorek
Nov 15 '18 at 4:19





1




1





I'm all for marking duplicates. But this one misses by a 6'2" ballerina split

– Abana Clara
Nov 15 '18 at 4:22





I'm all for marking duplicates. But this one misses by a 6'2" ballerina split

– Abana Clara
Nov 15 '18 at 4:22




2




2





Weird duplicate vote... Either pick this or this as dupe.

– KarelG
Nov 15 '18 at 7:54






Weird duplicate vote... Either pick this or this as dupe.

– KarelG
Nov 15 '18 at 7:54













5 Answers
5






active

oldest

votes


















3














Just use split with trim.






var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);








share|improve this answer




















  • 3





    Why include jquery?

    – dotconnor
    Nov 15 '18 at 4:12






  • 1





    This will error when there is no comma. And may not work as expected if there are many commas.

    – Spencer Wieczorek
    Nov 15 '18 at 4:14












  • waaa thank you very much

    – senaa
    Nov 15 '18 at 4:14


















3














You can use String.prototype.split():




The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.




You can use length property of the generated array as the last index to access the string item. Finally trim() the string:






var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);








share|improve this answer
































    3














    You can use string.lastIndexOf() to pull the last word out without making a new array:






    let accountcode = "xxxx, hello";
    let lastCommaIndex = accountcode.lastIndexOf(',')
    let word = accountcode.slice(lastCommaIndex+1).trim()
    console.log(word)








    share|improve this answer
































      2














      You can split the String on the comma.






      var s = 'xxxx, hello';
      var parts = s.split(',');
      console.log(parts[1]);





      If you don't want any leading or trailing spaces, use trim.






      var s = 'xxxx, hello';
      var parts = s.split(',');
      console.log(parts[1].trim());








      share|improve this answer
































        0














        accountcode = "xxxx, hello";
        let macthed=accountcode.match(/w+$/)
        if(matched)
        document.write(matched[0])



        here w+ means any one or more charecter
        and $ meand end of string
        so w+$ means get all the character upto end of the sting
        so here ' ' space is not a whole character so it started after space upto $



        the if statement is required because if no match found than macthed will be null , and it found it will be an array and first element will be your match






        share|improve this answer





























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Just use split with trim.






          var accountcode = "xxxx, tes";
          var result= accountcode.split(',')[1].trim();
          console.log(result);








          share|improve this answer




















          • 3





            Why include jquery?

            – dotconnor
            Nov 15 '18 at 4:12






          • 1





            This will error when there is no comma. And may not work as expected if there are many commas.

            – Spencer Wieczorek
            Nov 15 '18 at 4:14












          • waaa thank you very much

            – senaa
            Nov 15 '18 at 4:14















          3














          Just use split with trim.






          var accountcode = "xxxx, tes";
          var result= accountcode.split(',')[1].trim();
          console.log(result);








          share|improve this answer




















          • 3





            Why include jquery?

            – dotconnor
            Nov 15 '18 at 4:12






          • 1





            This will error when there is no comma. And may not work as expected if there are many commas.

            – Spencer Wieczorek
            Nov 15 '18 at 4:14












          • waaa thank you very much

            – senaa
            Nov 15 '18 at 4:14













          3












          3








          3







          Just use split with trim.






          var accountcode = "xxxx, tes";
          var result= accountcode.split(',')[1].trim();
          console.log(result);








          share|improve this answer















          Just use split with trim.






          var accountcode = "xxxx, tes";
          var result= accountcode.split(',')[1].trim();
          console.log(result);








          var accountcode = "xxxx, tes";
          var result= accountcode.split(',')[1].trim();
          console.log(result);





          var accountcode = "xxxx, tes";
          var result= accountcode.split(',')[1].trim();
          console.log(result);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 5:13









          Abana Clara

          1,646919




          1,646919










          answered Nov 15 '18 at 4:11









          ShreeShree

          12.8k2071123




          12.8k2071123







          • 3





            Why include jquery?

            – dotconnor
            Nov 15 '18 at 4:12






          • 1





            This will error when there is no comma. And may not work as expected if there are many commas.

            – Spencer Wieczorek
            Nov 15 '18 at 4:14












          • waaa thank you very much

            – senaa
            Nov 15 '18 at 4:14












          • 3





            Why include jquery?

            – dotconnor
            Nov 15 '18 at 4:12






          • 1





            This will error when there is no comma. And may not work as expected if there are many commas.

            – Spencer Wieczorek
            Nov 15 '18 at 4:14












          • waaa thank you very much

            – senaa
            Nov 15 '18 at 4:14







          3




          3





          Why include jquery?

          – dotconnor
          Nov 15 '18 at 4:12





          Why include jquery?

          – dotconnor
          Nov 15 '18 at 4:12




          1




          1





          This will error when there is no comma. And may not work as expected if there are many commas.

          – Spencer Wieczorek
          Nov 15 '18 at 4:14






          This will error when there is no comma. And may not work as expected if there are many commas.

          – Spencer Wieczorek
          Nov 15 '18 at 4:14














          waaa thank you very much

          – senaa
          Nov 15 '18 at 4:14





          waaa thank you very much

          – senaa
          Nov 15 '18 at 4:14













          3














          You can use String.prototype.split():




          The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.




          You can use length property of the generated array as the last index to access the string item. Finally trim() the string:






          var s = 'xxxx, hello';
          s = s.split(',');
          s = s[s.length - 1].trim();
          document.write(s);








          share|improve this answer





























            3














            You can use String.prototype.split():




            The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.




            You can use length property of the generated array as the last index to access the string item. Finally trim() the string:






            var s = 'xxxx, hello';
            s = s.split(',');
            s = s[s.length - 1].trim();
            document.write(s);








            share|improve this answer



























              3












              3








              3







              You can use String.prototype.split():




              The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.




              You can use length property of the generated array as the last index to access the string item. Finally trim() the string:






              var s = 'xxxx, hello';
              s = s.split(',');
              s = s[s.length - 1].trim();
              document.write(s);








              share|improve this answer















              You can use String.prototype.split():




              The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.




              You can use length property of the generated array as the last index to access the string item. Finally trim() the string:






              var s = 'xxxx, hello';
              s = s.split(',');
              s = s[s.length - 1].trim();
              document.write(s);








              var s = 'xxxx, hello';
              s = s.split(',');
              s = s[s.length - 1].trim();
              document.write(s);





              var s = 'xxxx, hello';
              s = s.split(',');
              s = s[s.length - 1].trim();
              document.write(s);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 8:06

























              answered Nov 15 '18 at 4:14









              MamunMamun

              28.5k71831




              28.5k71831





















                  3














                  You can use string.lastIndexOf() to pull the last word out without making a new array:






                  let accountcode = "xxxx, hello";
                  let lastCommaIndex = accountcode.lastIndexOf(',')
                  let word = accountcode.slice(lastCommaIndex+1).trim()
                  console.log(word)








                  share|improve this answer





























                    3














                    You can use string.lastIndexOf() to pull the last word out without making a new array:






                    let accountcode = "xxxx, hello";
                    let lastCommaIndex = accountcode.lastIndexOf(',')
                    let word = accountcode.slice(lastCommaIndex+1).trim()
                    console.log(word)








                    share|improve this answer



























                      3












                      3








                      3







                      You can use string.lastIndexOf() to pull the last word out without making a new array:






                      let accountcode = "xxxx, hello";
                      let lastCommaIndex = accountcode.lastIndexOf(',')
                      let word = accountcode.slice(lastCommaIndex+1).trim()
                      console.log(word)








                      share|improve this answer















                      You can use string.lastIndexOf() to pull the last word out without making a new array:






                      let accountcode = "xxxx, hello";
                      let lastCommaIndex = accountcode.lastIndexOf(',')
                      let word = accountcode.slice(lastCommaIndex+1).trim()
                      console.log(word)








                      let accountcode = "xxxx, hello";
                      let lastCommaIndex = accountcode.lastIndexOf(',')
                      let word = accountcode.slice(lastCommaIndex+1).trim()
                      console.log(word)





                      let accountcode = "xxxx, hello";
                      let lastCommaIndex = accountcode.lastIndexOf(',')
                      let word = accountcode.slice(lastCommaIndex+1).trim()
                      console.log(word)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 19 '18 at 2:03









                      hev1

                      5,8733529




                      5,8733529










                      answered Nov 15 '18 at 4:14









                      Mark MeyerMark Meyer

                      38.9k33160




                      38.9k33160





















                          2














                          You can split the String on the comma.






                          var s = 'xxxx, hello';
                          var parts = s.split(',');
                          console.log(parts[1]);





                          If you don't want any leading or trailing spaces, use trim.






                          var s = 'xxxx, hello';
                          var parts = s.split(',');
                          console.log(parts[1].trim());








                          share|improve this answer





























                            2














                            You can split the String on the comma.






                            var s = 'xxxx, hello';
                            var parts = s.split(',');
                            console.log(parts[1]);





                            If you don't want any leading or trailing spaces, use trim.






                            var s = 'xxxx, hello';
                            var parts = s.split(',');
                            console.log(parts[1].trim());








                            share|improve this answer



























                              2












                              2








                              2







                              You can split the String on the comma.






                              var s = 'xxxx, hello';
                              var parts = s.split(',');
                              console.log(parts[1]);





                              If you don't want any leading or trailing spaces, use trim.






                              var s = 'xxxx, hello';
                              var parts = s.split(',');
                              console.log(parts[1].trim());








                              share|improve this answer















                              You can split the String on the comma.






                              var s = 'xxxx, hello';
                              var parts = s.split(',');
                              console.log(parts[1]);





                              If you don't want any leading or trailing spaces, use trim.






                              var s = 'xxxx, hello';
                              var parts = s.split(',');
                              console.log(parts[1].trim());








                              var s = 'xxxx, hello';
                              var parts = s.split(',');
                              console.log(parts[1]);





                              var s = 'xxxx, hello';
                              var parts = s.split(',');
                              console.log(parts[1]);





                              var s = 'xxxx, hello';
                              var parts = s.split(',');
                              console.log(parts[1].trim());





                              var s = 'xxxx, hello';
                              var parts = s.split(',');
                              console.log(parts[1].trim());






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 15 '18 at 4:19

























                              answered Nov 15 '18 at 4:12









                              hev1hev1

                              5,8733529




                              5,8733529





















                                  0














                                  accountcode = "xxxx, hello";
                                  let macthed=accountcode.match(/w+$/)
                                  if(matched)
                                  document.write(matched[0])



                                  here w+ means any one or more charecter
                                  and $ meand end of string
                                  so w+$ means get all the character upto end of the sting
                                  so here ' ' space is not a whole character so it started after space upto $



                                  the if statement is required because if no match found than macthed will be null , and it found it will be an array and first element will be your match






                                  share|improve this answer



























                                    0














                                    accountcode = "xxxx, hello";
                                    let macthed=accountcode.match(/w+$/)
                                    if(matched)
                                    document.write(matched[0])



                                    here w+ means any one or more charecter
                                    and $ meand end of string
                                    so w+$ means get all the character upto end of the sting
                                    so here ' ' space is not a whole character so it started after space upto $



                                    the if statement is required because if no match found than macthed will be null , and it found it will be an array and first element will be your match






                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      accountcode = "xxxx, hello";
                                      let macthed=accountcode.match(/w+$/)
                                      if(matched)
                                      document.write(matched[0])



                                      here w+ means any one or more charecter
                                      and $ meand end of string
                                      so w+$ means get all the character upto end of the sting
                                      so here ' ' space is not a whole character so it started after space upto $



                                      the if statement is required because if no match found than macthed will be null , and it found it will be an array and first element will be your match






                                      share|improve this answer













                                      accountcode = "xxxx, hello";
                                      let macthed=accountcode.match(/w+$/)
                                      if(matched)
                                      document.write(matched[0])



                                      here w+ means any one or more charecter
                                      and $ meand end of string
                                      so w+$ means get all the character upto end of the sting
                                      so here ' ' space is not a whole character so it started after space upto $



                                      the if statement is required because if no match found than macthed will be null , and it found it will be an array and first element will be your match







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 15 '18 at 4:19









                                      Pranoy SarkarPranoy Sarkar

                                      583312




                                      583312













                                          這個網誌中的熱門文章

                                          Barbados

                                          How to read a connectionString WITH PROVIDER in .NET Core?

                                          Node.js Script on GitHub Pages or Amazon S3