How to check if a string contains an element from a list
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
add a comment |
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
add a comment |
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
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
apex string contains
asked Dec 9 at 13:57
Salvation
656
656
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
Thank you sfdcfox !!
– Salvation
Dec 10 at 6:13
add a comment |
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;
}
}
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
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%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
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.
Thank you sfdcfox !!
– Salvation
Dec 10 at 6:13
add a comment |
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.
Thank you sfdcfox !!
– Salvation
Dec 10 at 6:13
add a comment |
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.
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.
answered Dec 9 at 14:17
sfdcfox
247k11188424
247k11188424
Thank you sfdcfox !!
– Salvation
Dec 10 at 6:13
add a comment |
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
add a comment |
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;
}
}
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
add a comment |
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;
}
}
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
add a comment |
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;
}
}
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;
}
}
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
add a comment |
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
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.
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.
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%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
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