Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
am writing a code, where i need to compare the name which contains some specific letters. what is the syntax for it?
Account acc;
(if acc.Name Contains 'ABC')
{
}
Use the indexOf method on String.
public with sharing class TestApex123{ static testMethod void testStringContainsTrue() { Account account = new Account(); account.Name = 'This is a Test Name'; String testString = 'Test'; if(account.Name != null && account.Name.indexOf(testString) != -1) { system.assert(true); } } static testMethod void testStringContainsFalse() { Account account = new Account(); account.Name = 'This is a Test Name'; String testString = 'NOTVALID'; if(account.Name != null && account.Name.indexOf(testString) == -1) { system.assert(true); } }}
Actually Apex strings do have a contains method, unlike a lot of programming languages. You use it like so:
Boolean result=myString.contains('FORCE');
and it returns true if the sequence of characters appears in the String.
Thank you.
It worked. :-)
Use the indexOf method on String.
public with sharing class TestApex123
{
static testMethod void testStringContainsTrue()
{
Account account = new Account();
account.Name = 'This is a Test Name';
String testString = 'Test';
if(account.Name != null && account.Name.indexOf(testString) != -1)
{
system.assert(true);
}
}
static testMethod void testStringContainsFalse()
{
Account account = new Account();
account.Name = 'This is a Test Name';
String testString = 'NOTVALID';
if(account.Name != null && account.Name.indexOf(testString) == -1)
{
system.assert(true);
}
}
}
Actually Apex strings do have a contains method, unlike a lot of programming languages. You use it like so:
and it returns true if the sequence of characters appears in the String.
Thank you.
It worked. :-)
if(!newAccount.Name.Contains('ABC'))
{
newAccount.Name = newAccount.Name +' '+ 'ABCD';
}
}
Please mark as best answer, if valuable for you.
You can use == to check the exact/100% match. For example,
if('BITEST' == 'BITEST') // or whatever varaible you have defined the other substring on.