目录

Apex - Strings

与任何其他编程语言一样,Apex中的字符串是任何没有字符限制的字符集。

Example

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

字符串的方法 (String Methods)

Salesforce中的String类有许多方法。 我们将在本章中介绍一些最重要和最常用的字符串方法。

contains

如果给定的字符串包含提到的子字符串,则此方法将返回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);

equals

如果给定字符串和方法中传递的字符串具有相同的二进制字符序列且它们不为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);

remove

此方法从给定字符串中删除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);

startsWith

如果给定的字符串以方法中提供的前缀开头,则此方法将返回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);
<上一篇.Apex - 变量
Apex - Arrays.下一篇>
↑回到顶部↑
WIKI教程 @2018