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;
}







1















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


enter image description here










share|improve this question



























  • don't you want Name IN ('a','b','c') ? or why not use apex bind variables?

    – cropredy
    8 hours ago




















1















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


enter image description here










share|improve this question



























  • don't you want Name IN ('a','b','c') ? or why not use apex bind variables?

    – cropredy
    8 hours ago
















1












1








1








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


enter image description here










share|improve this question
















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


enter image description here







apex lightning






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago







Ou Zhang

















asked 8 hours ago









Ou ZhangOu Zhang

344 bronze badges




344 bronze badges
















  • 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



















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












3 Answers
3






active

oldest

votes


















5
















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 as apex:outputText and
the Java MessageFormat 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'; */





share|improve this answer




























  • 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



















3
















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)






share|improve this answer


























  • Sorry, I did not express it clearly. I updated the question, can you help me again?

    – Ou Zhang
    7 hours ago



















1
















If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');.






share|improve this answer






























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


    }
    });















    draft saved

    draft discarded
















    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









    5
















    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 as apex:outputText and
    the Java MessageFormat 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'; */





    share|improve this answer




























    • 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
















    5
















    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 as apex:outputText and
    the Java MessageFormat 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'; */





    share|improve this answer




























    • 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














    5














    5










    5









    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 as apex:outputText and
    the Java MessageFormat 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'; */





    share|improve this answer















    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 as apex:outputText and
    the Java MessageFormat 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'; */






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • 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













    3
















    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)






    share|improve this answer


























    • Sorry, I did not express it clearly. I updated the question, can you help me again?

      – Ou Zhang
      7 hours ago
















    3
















    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)






    share|improve this answer


























    • Sorry, I did not express it clearly. I updated the question, can you help me again?

      – Ou Zhang
      7 hours ago














    3














    3










    3









    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)






    share|improve this answer













    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)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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











    1
















    If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');.






    share|improve this answer
































      1
















      If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');.






      share|improve this answer






























        1














        1










        1









        If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');.






        share|improve this answer















        If you are trying to store a list of conditions and add them to a soql query you can try String.join(strList, ' OR ');.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        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


































            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%2f276105%2fin-apex-how-to-replace-the-value-in-the-string%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

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m