How to check if a string contains an element from a list












3














What is the best way to check if a string contains an element from a list?



for example:



String email= 'itairu@gmail.com'
List<String> domainNames = split by ; from domains in a custom setting


for example : (domainNames = [0] - gmail.com, [1] - example.com, [2] - mof.com);



How can I check if email field is contains one of the values in the domainName's List?



I can do it with a Boolean field that will be turned to true if one of field is found for this case -



boolean found = false;
for(String s : domainNames ){
if(email.contains(s)){
found =true;
}

}


I want to know if there is a better way - in case I will have a huge amount of values to move on in the for loop for this.



Thanks!










share|improve this question



























    3














    What is the best way to check if a string contains an element from a list?



    for example:



    String email= 'itairu@gmail.com'
    List<String> domainNames = split by ; from domains in a custom setting


    for example : (domainNames = [0] - gmail.com, [1] - example.com, [2] - mof.com);



    How can I check if email field is contains one of the values in the domainName's List?



    I can do it with a Boolean field that will be turned to true if one of field is found for this case -



    boolean found = false;
    for(String s : domainNames ){
    if(email.contains(s)){
    found =true;
    }

    }


    I want to know if there is a better way - in case I will have a huge amount of values to move on in the for loop for this.



    Thanks!










    share|improve this question

























      3












      3








      3







      What is the best way to check if a string contains an element from a list?



      for example:



      String email= 'itairu@gmail.com'
      List<String> domainNames = split by ; from domains in a custom setting


      for example : (domainNames = [0] - gmail.com, [1] - example.com, [2] - mof.com);



      How can I check if email field is contains one of the values in the domainName's List?



      I can do it with a Boolean field that will be turned to true if one of field is found for this case -



      boolean found = false;
      for(String s : domainNames ){
      if(email.contains(s)){
      found =true;
      }

      }


      I want to know if there is a better way - in case I will have a huge amount of values to move on in the for loop for this.



      Thanks!










      share|improve this question













      What is the best way to check if a string contains an element from a list?



      for example:



      String email= 'itairu@gmail.com'
      List<String> domainNames = split by ; from domains in a custom setting


      for example : (domainNames = [0] - gmail.com, [1] - example.com, [2] - mof.com);



      How can I check if email field is contains one of the values in the domainName's List?



      I can do it with a Boolean field that will be turned to true if one of field is found for this case -



      boolean found = false;
      for(String s : domainNames ){
      if(email.contains(s)){
      found =true;
      }

      }


      I want to know if there is a better way - in case I will have a huge amount of values to move on in the for loop for this.



      Thanks!







      apex string contains






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 9 at 13:57









      Salvation

      656




      656






















          2 Answers
          2






          active

          oldest

          votes


















          7














          A shorter version of your code just checks the domain directly:



          Boolean found = domainNames.contains(email.split('@',2)[1]);


          There's other ways to do this, too, but this is probably the most straightforward version.






          share|improve this answer





















          • Thank you sfdcfox !!
            – Salvation
            Dec 10 at 6:13



















          3














          If you have control over the type of domainNames, you should consider changing its type to Set<string>. Sets hash their values, so checking whether they contain a particular value takes the same amount of time regardless of the number of elements in the set, compared to a List which must go through each element until it finds one that matches.



          Set<String> domainNames = new Set<String>{'example.com', 'gmail.com'};
          domainNames.contains(email.split('@', 2)[1]);


          Also, the general solution to the question of whether a particular string contains any strings in a list where they could be in any part of the input string would be to do what you did, except you would use a break statement to end the loop once you've found a match:



          boolean found = false;
          for(String s : domainNames ){
          if(email.contains(s)){
          found = true;
          break;
          }
          }





          share|improve this answer





















          • Thanks a lot. Both of the answers helped me but unfortunately, I can give the best answer only to one.
            – Salvation
            Dec 10 at 6:11











          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%2f241902%2fhow-to-check-if-a-string-contains-an-element-from-a-list%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          A shorter version of your code just checks the domain directly:



          Boolean found = domainNames.contains(email.split('@',2)[1]);


          There's other ways to do this, too, but this is probably the most straightforward version.






          share|improve this answer





















          • Thank you sfdcfox !!
            – Salvation
            Dec 10 at 6:13
















          7














          A shorter version of your code just checks the domain directly:



          Boolean found = domainNames.contains(email.split('@',2)[1]);


          There's other ways to do this, too, but this is probably the most straightforward version.






          share|improve this answer





















          • Thank you sfdcfox !!
            – Salvation
            Dec 10 at 6:13














          7












          7








          7






          A shorter version of your code just checks the domain directly:



          Boolean found = domainNames.contains(email.split('@',2)[1]);


          There's other ways to do this, too, but this is probably the most straightforward version.






          share|improve this answer












          A shorter version of your code just checks the domain directly:



          Boolean found = domainNames.contains(email.split('@',2)[1]);


          There's other ways to do this, too, but this is probably the most straightforward version.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 9 at 14:17









          sfdcfox

          247k11188424




          247k11188424












          • Thank you sfdcfox !!
            – Salvation
            Dec 10 at 6:13


















          • Thank you sfdcfox !!
            – Salvation
            Dec 10 at 6:13
















          Thank you sfdcfox !!
          – Salvation
          Dec 10 at 6:13




          Thank you sfdcfox !!
          – Salvation
          Dec 10 at 6:13













          3














          If you have control over the type of domainNames, you should consider changing its type to Set<string>. Sets hash their values, so checking whether they contain a particular value takes the same amount of time regardless of the number of elements in the set, compared to a List which must go through each element until it finds one that matches.



          Set<String> domainNames = new Set<String>{'example.com', 'gmail.com'};
          domainNames.contains(email.split('@', 2)[1]);


          Also, the general solution to the question of whether a particular string contains any strings in a list where they could be in any part of the input string would be to do what you did, except you would use a break statement to end the loop once you've found a match:



          boolean found = false;
          for(String s : domainNames ){
          if(email.contains(s)){
          found = true;
          break;
          }
          }





          share|improve this answer





















          • Thanks a lot. Both of the answers helped me but unfortunately, I can give the best answer only to one.
            – Salvation
            Dec 10 at 6:11
















          3














          If you have control over the type of domainNames, you should consider changing its type to Set<string>. Sets hash their values, so checking whether they contain a particular value takes the same amount of time regardless of the number of elements in the set, compared to a List which must go through each element until it finds one that matches.



          Set<String> domainNames = new Set<String>{'example.com', 'gmail.com'};
          domainNames.contains(email.split('@', 2)[1]);


          Also, the general solution to the question of whether a particular string contains any strings in a list where they could be in any part of the input string would be to do what you did, except you would use a break statement to end the loop once you've found a match:



          boolean found = false;
          for(String s : domainNames ){
          if(email.contains(s)){
          found = true;
          break;
          }
          }





          share|improve this answer





















          • Thanks a lot. Both of the answers helped me but unfortunately, I can give the best answer only to one.
            – Salvation
            Dec 10 at 6:11














          3












          3








          3






          If you have control over the type of domainNames, you should consider changing its type to Set<string>. Sets hash their values, so checking whether they contain a particular value takes the same amount of time regardless of the number of elements in the set, compared to a List which must go through each element until it finds one that matches.



          Set<String> domainNames = new Set<String>{'example.com', 'gmail.com'};
          domainNames.contains(email.split('@', 2)[1]);


          Also, the general solution to the question of whether a particular string contains any strings in a list where they could be in any part of the input string would be to do what you did, except you would use a break statement to end the loop once you've found a match:



          boolean found = false;
          for(String s : domainNames ){
          if(email.contains(s)){
          found = true;
          break;
          }
          }





          share|improve this answer












          If you have control over the type of domainNames, you should consider changing its type to Set<string>. Sets hash their values, so checking whether they contain a particular value takes the same amount of time regardless of the number of elements in the set, compared to a List which must go through each element until it finds one that matches.



          Set<String> domainNames = new Set<String>{'example.com', 'gmail.com'};
          domainNames.contains(email.split('@', 2)[1]);


          Also, the general solution to the question of whether a particular string contains any strings in a list where they could be in any part of the input string would be to do what you did, except you would use a break statement to end the loop once you've found a match:



          boolean found = false;
          for(String s : domainNames ){
          if(email.contains(s)){
          found = true;
          break;
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 9 at 18:38









          IllusiveBrian

          3,143817




          3,143817












          • Thanks a lot. Both of the answers helped me but unfortunately, I can give the best answer only to one.
            – Salvation
            Dec 10 at 6:11


















          • Thanks a lot. Both of the answers helped me but unfortunately, I can give the best answer only to one.
            – Salvation
            Dec 10 at 6:11
















          Thanks a lot. Both of the answers helped me but unfortunately, I can give the best answer only to one.
          – Salvation
          Dec 10 at 6:11




          Thanks a lot. Both of the answers helped me but unfortunately, I can give the best answer only to one.
          – Salvation
          Dec 10 at 6:11


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f241902%2fhow-to-check-if-a-string-contains-an-element-from-a-list%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

          Bressuire

          Cabo Verde

          Gyllenstierna