In apex, how to replace the value in the stringReplacing string valueIssues with calling 2 setTest methods in...
How can I hint that my character isn't real?
Meaning of "Right Out" in Context
Can you create water inside someone's mouth?
Round away from zero
Add builder hat to other people with tikzpeople
How to measure the statistical "distance" between two frequency distributions?
Why Is Sojdlg123aljg a Common Password?
Examples where "thin + thin = nice and thick"
Is there some sort of French saying for "a person's signature move"?
Phrase request for "work in" in the context of gyms
Could a simple-majority bill for a general election, passing through both houses be amended by the SNP to provide for a further Scottish referendum?
Did the US Climate Reference Network Show No New Warming Since 2005 in the US?
Why does 8 bit truecolor use only 2 bits for blue?
What is the purpose of the rotating plate in front of the lock?
If I have an accident, should I file a claim with my car insurance company?
Professor refuses to write a recommendation letter to students who haven't written a research paper with him
First Number to Contain Each Letter
How to calculate the power level of a Commander deck?
Entering the US with dual citizenship but US passport is long expired?
What's this constructed number's starter?
Is Sanskrit really the mother of all languages?
Infinitely many primes
Who's this voice acting performer?
Draw the ☣ (Biohazard Symbol)
In apex, how to replace the value in the string
Replacing string valueIssues with calling 2 setTest methods in apex test classHow to extract a number from a stringproblem replacing and splitting stringAdd an String value to a Integer SelectOption list - convert String to IntegerCannot convert Boolean to StringVariable text string for SObject get method?Convert double to String
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
This is my test code
String soqlStr = '1 OR 2 AND 3';
List<String> strList = new List<String>();
strList.add('Name = a');
strList.add('City = b');
strList.add('Birthdate__c = c');
String s2 = '';
for (Integer i = 0; i < strList.size() ; i ++) {
String s1 = strList[i];
System.debug(LoggingLevel.INFO, 's1 = ' + s1);
s2 = soqlStr.replace(String.valueOf(i+1), s1);
System.debug(LoggingLevel.INFO, 's2 = ' + s2);
}
I want to get the result:
String soqlStr = 'Name = a OR City = b AND Birthdate__c = c';
apex lightning
add a comment |
This is my test code
String soqlStr = '1 OR 2 AND 3';
List<String> strList = new List<String>();
strList.add('Name = a');
strList.add('City = b');
strList.add('Birthdate__c = c');
String s2 = '';
for (Integer i = 0; i < strList.size() ; i ++) {
String s1 = strList[i];
System.debug(LoggingLevel.INFO, 's1 = ' + s1);
s2 = soqlStr.replace(String.valueOf(i+1), s1);
System.debug(LoggingLevel.INFO, 's2 = ' + s2);
}
I want to get the result:
String soqlStr = 'Name = a OR City = b AND Birthdate__c = c';
apex lightning
don't you wantName IN ('a','b','c')
? or why not use apex bind variables?
– cropredy
8 hours ago
add a comment |
This is my test code
String soqlStr = '1 OR 2 AND 3';
List<String> strList = new List<String>();
strList.add('Name = a');
strList.add('City = b');
strList.add('Birthdate__c = c');
String s2 = '';
for (Integer i = 0; i < strList.size() ; i ++) {
String s1 = strList[i];
System.debug(LoggingLevel.INFO, 's1 = ' + s1);
s2 = soqlStr.replace(String.valueOf(i+1), s1);
System.debug(LoggingLevel.INFO, 's2 = ' + s2);
}
I want to get the result:
String soqlStr = 'Name = a OR City = b AND Birthdate__c = c';
apex lightning
This is my test code
String soqlStr = '1 OR 2 AND 3';
List<String> strList = new List<String>();
strList.add('Name = a');
strList.add('City = b');
strList.add('Birthdate__c = c');
String s2 = '';
for (Integer i = 0; i < strList.size() ; i ++) {
String s1 = strList[i];
System.debug(LoggingLevel.INFO, 's1 = ' + s1);
s2 = soqlStr.replace(String.valueOf(i+1), s1);
System.debug(LoggingLevel.INFO, 's2 = ' + s2);
}
I want to get the result:
String soqlStr = 'Name = a OR City = b AND Birthdate__c = c';
apex lightning
apex lightning
edited 7 hours ago
Ou Zhang
asked 8 hours ago
Ou ZhangOu Zhang
344 bronze badges
344 bronze badges
don't you wantName IN ('a','b','c')
? or why not use apex bind variables?
– cropredy
8 hours ago
add a comment |
don't you wantName IN ('a','b','c')
? or why not use apex bind variables?
– cropredy
8 hours ago
don't you want
Name IN ('a','b','c')
? or why not use apex bind variables?– cropredy
8 hours ago
don't you want
Name IN ('a','b','c')
? or why not use apex bind variables?– cropredy
8 hours ago
add a comment |
3 Answers
3
active
oldest
votes
Specifically for your case - Derek's answer is absolutely correct. But in the general case, if we want to build a string by substitution the certain slots with arguments, it is convenient to use String.format()
method:
public static String format(String stringToFormat, List<Object>
formattingArguments);
Treat the first argument as a pattern and return
a string using the second argument for substitution and formatting.
The substitution and formatting are the same asapex:outputText
and
the JavaMessageFormat
class. Non-string types in the second
argument’s List are implicitly converted to strings, respecting the
toString()
method overrides that exist on the type.
String template = '{0} OR {1} AND {2}';
List<String> params = new List<String>();
params.add('Name = a');
params.add('City = b');
params.add('Birthdate__c = c');
String formattedTemplate = String.format(template, params);
/* String formattedTemplate = 'Name = a OR City = b AND Birthdate__c = c'; */
This is a great answer, thank you very much. But I still have a problem, after using [String.format], the last parameter never changes. Result: template2 = City = b OR Birthdate__c = c AND {3}
– Ou Zhang
7 hours ago
I've updated indexes in my answer, starting from 0. Check again, please. Just a typo
– Oleh Berehovskyi
7 hours ago
I understand, thank you very much.
– Ou Zhang
7 hours ago
add a comment |
Looks like you're trying to do things the hard way. SOQL provides a built-in way to do this using IN
.
ex.
List<String> accountNames = new List<String>{'Tech co', 'MegaCorp, utld.', 'Mom 'n Pop Shop'};
List<Account> accounts = [SELECT Id FROM Account WHERE Name IN :accountNames];
If this is for a dynamic query, you might have to do a little more work, but a little ingenuity with String.join()
should get you where you need to go in that case (still using the IN
keyword)
Sorry, I did not express it clearly. I updated the question, can you help me again?
– Ou Zhang
7 hours ago
add a comment |
If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');
.
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/4.0/"u003ecc by-sa 4.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%2f276105%2fin-apex-how-to-replace-the-value-in-the-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Specifically for your case - Derek's answer is absolutely correct. But in the general case, if we want to build a string by substitution the certain slots with arguments, it is convenient to use String.format()
method:
public static String format(String stringToFormat, List<Object>
formattingArguments);
Treat the first argument as a pattern and return
a string using the second argument for substitution and formatting.
The substitution and formatting are the same asapex:outputText
and
the JavaMessageFormat
class. Non-string types in the second
argument’s List are implicitly converted to strings, respecting the
toString()
method overrides that exist on the type.
String template = '{0} OR {1} AND {2}';
List<String> params = new List<String>();
params.add('Name = a');
params.add('City = b');
params.add('Birthdate__c = c');
String formattedTemplate = String.format(template, params);
/* String formattedTemplate = 'Name = a OR City = b AND Birthdate__c = c'; */
This is a great answer, thank you very much. But I still have a problem, after using [String.format], the last parameter never changes. Result: template2 = City = b OR Birthdate__c = c AND {3}
– Ou Zhang
7 hours ago
I've updated indexes in my answer, starting from 0. Check again, please. Just a typo
– Oleh Berehovskyi
7 hours ago
I understand, thank you very much.
– Ou Zhang
7 hours ago
add a comment |
Specifically for your case - Derek's answer is absolutely correct. But in the general case, if we want to build a string by substitution the certain slots with arguments, it is convenient to use String.format()
method:
public static String format(String stringToFormat, List<Object>
formattingArguments);
Treat the first argument as a pattern and return
a string using the second argument for substitution and formatting.
The substitution and formatting are the same asapex:outputText
and
the JavaMessageFormat
class. Non-string types in the second
argument’s List are implicitly converted to strings, respecting the
toString()
method overrides that exist on the type.
String template = '{0} OR {1} AND {2}';
List<String> params = new List<String>();
params.add('Name = a');
params.add('City = b');
params.add('Birthdate__c = c');
String formattedTemplate = String.format(template, params);
/* String formattedTemplate = 'Name = a OR City = b AND Birthdate__c = c'; */
This is a great answer, thank you very much. But I still have a problem, after using [String.format], the last parameter never changes. Result: template2 = City = b OR Birthdate__c = c AND {3}
– Ou Zhang
7 hours ago
I've updated indexes in my answer, starting from 0. Check again, please. Just a typo
– Oleh Berehovskyi
7 hours ago
I understand, thank you very much.
– Ou Zhang
7 hours ago
add a comment |
Specifically for your case - Derek's answer is absolutely correct. But in the general case, if we want to build a string by substitution the certain slots with arguments, it is convenient to use String.format()
method:
public static String format(String stringToFormat, List<Object>
formattingArguments);
Treat the first argument as a pattern and return
a string using the second argument for substitution and formatting.
The substitution and formatting are the same asapex:outputText
and
the JavaMessageFormat
class. Non-string types in the second
argument’s List are implicitly converted to strings, respecting the
toString()
method overrides that exist on the type.
String template = '{0} OR {1} AND {2}';
List<String> params = new List<String>();
params.add('Name = a');
params.add('City = b');
params.add('Birthdate__c = c');
String formattedTemplate = String.format(template, params);
/* String formattedTemplate = 'Name = a OR City = b AND Birthdate__c = c'; */
Specifically for your case - Derek's answer is absolutely correct. But in the general case, if we want to build a string by substitution the certain slots with arguments, it is convenient to use String.format()
method:
public static String format(String stringToFormat, List<Object>
formattingArguments);
Treat the first argument as a pattern and return
a string using the second argument for substitution and formatting.
The substitution and formatting are the same asapex:outputText
and
the JavaMessageFormat
class. Non-string types in the second
argument’s List are implicitly converted to strings, respecting the
toString()
method overrides that exist on the type.
String template = '{0} OR {1} AND {2}';
List<String> params = new List<String>();
params.add('Name = a');
params.add('City = b');
params.add('Birthdate__c = c');
String formattedTemplate = String.format(template, params);
/* String formattedTemplate = 'Name = a OR City = b AND Birthdate__c = c'; */
edited 7 hours ago
answered 7 hours ago
Oleh BerehovskyiOleh Berehovskyi
1,0861 gold badge4 silver badges16 bronze badges
1,0861 gold badge4 silver badges16 bronze badges
This is a great answer, thank you very much. But I still have a problem, after using [String.format], the last parameter never changes. Result: template2 = City = b OR Birthdate__c = c AND {3}
– Ou Zhang
7 hours ago
I've updated indexes in my answer, starting from 0. Check again, please. Just a typo
– Oleh Berehovskyi
7 hours ago
I understand, thank you very much.
– Ou Zhang
7 hours ago
add a comment |
This is a great answer, thank you very much. But I still have a problem, after using [String.format], the last parameter never changes. Result: template2 = City = b OR Birthdate__c = c AND {3}
– Ou Zhang
7 hours ago
I've updated indexes in my answer, starting from 0. Check again, please. Just a typo
– Oleh Berehovskyi
7 hours ago
I understand, thank you very much.
– Ou Zhang
7 hours ago
This is a great answer, thank you very much. But I still have a problem, after using [String.format], the last parameter never changes. Result: template2 = City = b OR Birthdate__c = c AND {3}
– Ou Zhang
7 hours ago
This is a great answer, thank you very much. But I still have a problem, after using [String.format], the last parameter never changes. Result: template2 = City = b OR Birthdate__c = c AND {3}
– Ou Zhang
7 hours ago
I've updated indexes in my answer, starting from 0. Check again, please. Just a typo
– Oleh Berehovskyi
7 hours ago
I've updated indexes in my answer, starting from 0. Check again, please. Just a typo
– Oleh Berehovskyi
7 hours ago
I understand, thank you very much.
– Ou Zhang
7 hours ago
I understand, thank you very much.
– Ou Zhang
7 hours ago
add a comment |
Looks like you're trying to do things the hard way. SOQL provides a built-in way to do this using IN
.
ex.
List<String> accountNames = new List<String>{'Tech co', 'MegaCorp, utld.', 'Mom 'n Pop Shop'};
List<Account> accounts = [SELECT Id FROM Account WHERE Name IN :accountNames];
If this is for a dynamic query, you might have to do a little more work, but a little ingenuity with String.join()
should get you where you need to go in that case (still using the IN
keyword)
Sorry, I did not express it clearly. I updated the question, can you help me again?
– Ou Zhang
7 hours ago
add a comment |
Looks like you're trying to do things the hard way. SOQL provides a built-in way to do this using IN
.
ex.
List<String> accountNames = new List<String>{'Tech co', 'MegaCorp, utld.', 'Mom 'n Pop Shop'};
List<Account> accounts = [SELECT Id FROM Account WHERE Name IN :accountNames];
If this is for a dynamic query, you might have to do a little more work, but a little ingenuity with String.join()
should get you where you need to go in that case (still using the IN
keyword)
Sorry, I did not express it clearly. I updated the question, can you help me again?
– Ou Zhang
7 hours ago
add a comment |
Looks like you're trying to do things the hard way. SOQL provides a built-in way to do this using IN
.
ex.
List<String> accountNames = new List<String>{'Tech co', 'MegaCorp, utld.', 'Mom 'n Pop Shop'};
List<Account> accounts = [SELECT Id FROM Account WHERE Name IN :accountNames];
If this is for a dynamic query, you might have to do a little more work, but a little ingenuity with String.join()
should get you where you need to go in that case (still using the IN
keyword)
Looks like you're trying to do things the hard way. SOQL provides a built-in way to do this using IN
.
ex.
List<String> accountNames = new List<String>{'Tech co', 'MegaCorp, utld.', 'Mom 'n Pop Shop'};
List<Account> accounts = [SELECT Id FROM Account WHERE Name IN :accountNames];
If this is for a dynamic query, you might have to do a little more work, but a little ingenuity with String.join()
should get you where you need to go in that case (still using the IN
keyword)
answered 8 hours ago
Derek FDerek F
23k6 gold badges26 silver badges55 bronze badges
23k6 gold badges26 silver badges55 bronze badges
Sorry, I did not express it clearly. I updated the question, can you help me again?
– Ou Zhang
7 hours ago
add a comment |
Sorry, I did not express it clearly. I updated the question, can you help me again?
– Ou Zhang
7 hours ago
Sorry, I did not express it clearly. I updated the question, can you help me again?
– Ou Zhang
7 hours ago
Sorry, I did not express it clearly. I updated the question, can you help me again?
– Ou Zhang
7 hours ago
add a comment |
If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');
.
add a comment |
If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');
.
add a comment |
If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');
.
If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');
.
edited 8 hours ago
Derek F
23k6 gold badges26 silver badges55 bronze badges
23k6 gold badges26 silver badges55 bronze badges
answered 8 hours ago
vm53vm53
293 bronze badges
293 bronze badges
add a comment |
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%2f276105%2fin-apex-how-to-replace-the-value-in-the-string%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
don't you want
Name IN ('a','b','c')
? or why not use apex bind variables?– cropredy
8 hours ago