Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Last active June 27, 2016 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyoshikawa1106/40fe7144fe6051da931d5dadfe7df4da to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/40fe7144fe6051da931d5dadfe7df4da to your computer and use it in GitHub Desktop.
Apex : String.format Demo
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