Apex - 字符串

2019-10-26 16:26 更新

與任何其他編程語(yǔ)言一樣,Apex中的字符串是沒有字符限制的任何字符集。


示例:

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


字符串方法

Salesforce中的String類有許多方法。 我們將看看本章中一些最重要和最常用的字符串方法。

contains 包含

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

語(yǔ)法:
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

如果給定的字符串和在方法中傳遞的字符串具有相同的二進(jìn)制字符序列,并且它們不為空,那么此方法將返回true。 您也可以使用此方法比較SFDC記錄ID。 此方法區(qū)分大小寫。

語(yǔ)法:
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。 但是,此方法不區(qū)分大小寫。

句法:
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中提供的字符串。 當(dāng)您想從字符串中刪除某些特定字符,并且不知道要?jiǎng)h除的字符的確切索引時(shí),這很有用。 此方法區(qū)分大小寫,如果出現(xiàn)相同的字符序列但是情況不同,則此方法將不起作用。

語(yǔ)法:
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中從給定字符串中提取的字符串,但前提是它出現(xiàn)在結(jié)尾。 此方法不區(qū)分大小寫。

語(yǔ)法:
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。

語(yǔ)法:
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);

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)