Apex-文字列

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

Example

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

文字列メソッド

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

含まれています

指定された文字列に指定された部分文字列が含まれている場合、このメソッドはtrueを返します。

Syntax

public Boolean contains(String substring)

Example

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を比較することもできます。この方法では大文字と小文字が区別されます。

Syntax

public Boolean equals(Object string)

Example

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を返します。ただし、この方法では大文字と小文字は区別されません。

Syntax

public Boolean equalsIgnoreCase(String stringtoCompare)

Example

次のコードは、大文字と小文字の区別を無視して、文字列文字とシーケンスが同じであるため、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で指定された文字列を指定された文字列から削除します。これは、文字列から特定の文字を削除する必要があり、削除する文字の正確なインデックスがわからない場合に役立ちます。この方法では大文字と小文字が区別され、同じ文字シーケンスが発生しても大文字と小文字が異なる場合は機能しません。

Syntax

public String remove(String stringToRemove)

Example

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で指定された文字列を指定された文字列から削除しますが、それが最後に発生した場合に限ります。この方法では大文字と小文字は区別されません。

Syntax

public String removeEndIgnoreCase(String stringToRemove)

Example

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

startWith

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

Syntax

public Boolean startsWith(String prefix)

Example

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