Last active
June 27, 2016 06:51
-
-
Save tyoshikawa1106/40fe7144fe6051da931d5dadfe7df4da to your computer and use it in GitHub Desktop.
Apex : String.format Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class StringFormatDemoController { | |
public StringFormatDemoController() { | |
this.demo01(); | |
this.demo02(); | |
} | |
private void demo01() { | |
String placeholder = 'Hello {0}, {1} is cool!'; | |
List<String> fillers = new String[]{'Jason','Apex'}; | |
String formatted = String.format(placeholder, fillers); | |
System.assertEquals('Hello Jason, Apex is cool!', formatted); | |
System.debug('demo1 = ' + formatted); | |
} | |
private void demo02() { | |
// SELECT項目 | |
List<String> selectFields = new List<String>(); | |
selectFields.add('Id'); | |
selectFields.add('Name'); | |
selectFields.add('AccountNumber'); | |
selectFields.add('Site'); | |
System.assertEquals(selectFields.size(), 4); | |
// 差し込みリスト | |
List<String> selectQueries = new List<String>(); | |
for (Integer i = 0; i < selectFields.size(); i++) { | |
// {0} {1} {2} {3} | |
selectQueries.add('{' + String.valueOf(i) + '}'); | |
} | |
// Query | |
String query = ''; | |
query += ' SELECT '; | |
query += String.join(selectQueries, ','); | |
query += ' FROM '; | |
query += ' Account '; | |
query += ' LIMIT 200 '; | |
String formattedQuery = String.format(query, selectFields); | |
List<Account> results = Database.query(formattedQuery); | |
System.debug('demo02 = ' + formattedQuery); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment