【Salesforce】文字列の比較について

【Salesforce】文字列の比較について

Apexでプログラムを作成しているとき、普段は文字列の比較にStringクラスのequalsを使用しています。

しかし、別の方が作成したソースを見てみると、文字列の比較を比較演算子の=でやっている人もいるようです。

https://developer.salesforce.com/docs/atlas.ja-jp.apexcode.meta/apexcode/apex_methods_system_string.htm

たしかにApexの文字列を比較する際に=で比較をすることは出来るのですが、比較演算子を使用した場合には大文字と小文字を区別しないそうです。

https://developer.salesforce.com/docs/atlas.ja-jp.apexcode.meta/apexcode/langCon_apex_primitives.htm

文字列の比較、Setのcontains、MapのcontainsKeyについて確認したのでメモです。

Set<String> strSet;
strSet = new Set<String>();
strSet.add('AA');

Map<String, Boolean> strMap;
strMap = new Map<String, Boolean>();
strMap.put('AA', true);

system.debug('【ログ】' + ('AA' == 'aa'));           // true
system.debug('【ログ】' + ('AA'.equals('aa')));      // false

system.debug('【ログ】' + strSet.contains('AA'));    // true
system.debug('【ログ】' + strSet.contains('aa'));    // false

system.debug('【ログ】' + strMap.containsKey('AA')); // true
system.debug('【ログ】' + strMap.containsKey('aa')); // false

Stringの比較演算子以外はすべて大文字と小文字を区別しているようです。

大文字・小文字を区別せずに使用する場合はStringクラスのtoLowerCase等を利用する必要がありそうです。

No comments.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です