Apex-strings

提供:Dev Guides
移動先:案内検索

Apex-文字列

Apexの文字列は、他のプログラミング言語と同様に、文字数制限のない任意の文字セットです。

String companyName = 'Abc International';
System.debug('Value companyName variable'+companyName);

文字列メソッド

Salesforceの文字列クラスには多くのメソッドがあります。 この章では、最も重要で頻繁に使用される文字列メソッドのいくつかを見ていきます。

含む

このメソッドは、指定された文字列に上記のサブストリングが含まれている場合にtrueを返します。

構文

public Boolean contains(String substring)

String myProductName1 = 'HCL';
String myProductName2 = 'NAHCL';
Boolean result = myProductName2.contains(myProductName1);
System.debug('O/p will be true as it contains the String and Output is:'+result);

等しい

このメソッドは、指定された文字列とメソッドで渡された文字列が同じ文字のバイナリシーケンスを持ち、それらがnullでない場合にtrueを返します。 この方法を使用して、SFDCレコードIDも比較できます。 このメソッドは大文字と小文字を区別します。

構文

public Boolean equals(Object string)

String myString1 = 'MyString';
String myString2 = 'MyString';
Boolean result = myString2.equals(myString1);
System.debug('Value of Result will be true as they are same and Result is:'+result);

equalsIgnoreCase

stringtoCompareに指定された文字列と同じ文字シーケンスがある場合、このメソッドはtrueを返します。 ただし、このメソッドは大文字と小文字を区別しません。

構文

public Boolean equalsIgnoreCase(String stringtoCompare)

次のコードは、文字列の文字とシーケンスが同じであるため、大文字と小文字の区別を無視してtrueを返します。

String myString1 = 'MySTRING';
String myString2 = 'MyString';
Boolean result = myString2.equalsIgnoreCase(myString1);
System.debug('Value of Result will be true as they are same and Result is:'+result);

削除する

このメソッドは、指定された文字列からstringToRemoveで提供される文字列を削除します。 これは、文字列から特定の文字を削除したいが、削除する文字の正確なインデックスを知らない場合に便利です。 このメソッドは大文字と小文字を区別し、同じ文字シーケンスが発生しても大文字と小文字が異なる場合は機能しません。

構文

public String remove(String stringToRemove)

String myString1 = 'This Is MyString Example';
String stringToRemove = 'MyString';
String result = myString1.remove(stringToRemove);
System.debug('Value of Result will be 'This Is Example' as we have removed the MyString
   and Result is :'+result);

removeEndIgnoreCase

このメソッドは、指定された文字列からstringToRemoveで提供された文字列を削除しますが、最後に発生した場合のみです。 このメソッドは大文字と小文字を区別しません。

構文

public String removeEndIgnoreCase(String stringToRemove)

String myString1 = 'This Is MyString EXAMPLE';
String stringToRemove = 'Example';
String result = myString1.removeEndIgnoreCase(stringToRemove);
System.debug('Value of Result will be 'This Is MyString' as we have removed the 'Example'
   and Result is :'+result);

startsWith

指定された文字列がメソッドで指定されたプレフィックスで始まる場合、このメソッドはtrueを返します。

構文

public Boolean startsWith(String prefix)

String myString1 = 'This Is MyString EXAMPLE';
String prefix = 'This';
Boolean result = myString1.startsWith(prefix);
System.debug(' This will return true as our String starts with string 'This' and the
   Result is :'+result);