课程表

Apex课程

工具箱
速查手册

Apex - 字符串

当前位置:免费教程 » 程序设计 » Apex

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


示例:

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


字符串方法

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

contains 包含

如果给定的字符串包含所提到的子字符串,此方法将返回true。

语法:
  1. public Boolean contains(String substring)

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

equals

如果给定的字符串和在方法中传递的字符串具有相同的二进制字符序列,并且它们不为空,那么此方法将返回true。 您也可以使用此方法比较SFDC记录ID。 此方法区分大小写。

语法:
  1. public Boolean equals(Object string)

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


equalsIgnoreCase

如果字符串Compare具有与给定字符串相同的字符序列,则此方法将返回true。 但是,此方法不区分大小写。

句法:
  1. public Boolean equalsIgnoreCase(String stringtoCompare)

示例:
下面的代码将返回true作为字符串字符和顺序是相同的,忽略大小写的敏感性。
  1. String myString1 = 'MySTRING';
  2. String myString2 = 'MyString';
  3. Boolean result = myString2.equalsIgnoreCase(myString1);
  4. System.debug('Value of Result will be true as they are same and Result is:'+result);

remove 删除

此方法从给定字符串中删除stringToRemove中提供的字符串。 当您想从字符串中删除某些特定字符,并且不知道要删除的字符的确切索引时,这很有用。 此方法区分大小写,如果出现相同的字符序列但是情况不同,则此方法将不起作用。

语法:
  1. public String remove(String stringToRemove)

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

removeEndIgnoreCase

此方法删除stringToRemove中从给定字符串中提取的字符串,但前提是它出现在结尾。 此方法不区分大小写。

语法:
  1. public String removeEndIgnoreCase(String stringToRemove)

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

startWith

如果给定的字符串以方法中提供的前缀开头,此方法将返回true。

语法:
  1. public Boolean startsWith(String prefix)

示例:
  1. String myString1 = 'This Is MyString EXAMPLE';
  2. String prefix = 'This';
  3. Boolean result = myString1.startsWith(prefix);
  4. System.debug(' This will return true as our String starts with string 'This' and the Result is :'+result);
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号