×
Apex 编程教程Apex 环境Apex 示例Apex 数据类型Apex 变量Apex 字符串Apex 数组Apex 常量Apex 决策Apex 循环Apex 集合Apex 类Apex 类方法Apex 对象Apex 接口Apex DMLApex 数据库方法Apex SOSLApex SOQLApex 安全性Apex 调用Apex 触发器Apex 触发设计模式Governer Limits调节器限制Apex 批量处理Apex 调试Apex 测试Apex 部署Apex 字符串Apex 数组

Apex 字符串


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


示例:

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


字符串方法

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

contains 包含

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

equals

如果给定的字符串和在方法中传递的字符串具有相同的二进制字符序列,并且它们不为空,那么此方法将返回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

如果字符串Compare具有与给定字符串相同的字符序列,则此方法将返回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);

remove 删除

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

startWith

如果给定的字符串以方法中提供的前缀开头,此方法将返回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);


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)