Understanding the String Manipulation, Arrays and Constants in Apex programming language

Last updated on Nov 24 2021
Abha Kulkarni

Table of Contents

Understanding the String Manipulation, Arrays and Constants in Apex programming language

Apex – Strings

String in Apex, as in the other programing language, is any set of characters with no character limit.
Example

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

String Methods

String class in Salesforce has many methods. we’ll take a glance at a number of the foremost important and regularly used string methods during this chapter.
contains
This method will return true if the given string contains the substring mentioned.
Syntax
public Boolean contains(String substring)
Example

String myProductName1 = 'HCL';
String myProductName2 = 'NAHCL';
Boolean result = myProductName2.contains(myProductName1);
System.debug('O/p are going to be true because it contains the String and Output is:'+result);

equals
This method will return true if the given string and therefore the string passed within the method have an equivalent binary sequence of characters and that they aren’t null. you’ll compare the SFDC record id also using this method. This method is case-sensitive.
Syntax
public Boolean equals(Object string)
Example

String myString1 = 'MyString';
String myString2 = 'MyString';
Boolean result = myString2.equals(myString1);
System.debug('Value of Result are going to be true as they're same and Result is:'+result);

equalsIgnoreCase
This method will return true if stringtoCompare has an equivalent sequence of characters because the given string. However, this method isn’t case-sensitive.
Syntax
public Boolean equalsIgnoreCase(String stringtoCompare)
Example
The following code will return true as string characters and sequence are same, ignoring the case sensitivity.

String myString1 = 'MySTRING';
String myString2 = 'MyString';
Boolean result = myString2.equalsIgnoreCase(myString1);
System.debug('Value of Result are going to be true as they're same and Result is:'+result);

remove
This method removes the string provided in stringToRemove from the given string. this is often useful once you want to get rid of some specific characters from string and aren’t conscious of the precise index of the characters to get rid of . This method is case sensitive and can not work if an equivalent character sequence occurs but case is different.
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 are going to be 'This Is Example' as we've removed the MyString 
and result's :'+result);

removeEndIgnoreCase
This method removes the string provided in stringToRemove from the given string but as long as it occurs at the top . This method isn’t case-sensitive.
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 are going to be 'This Is MyString' as we've removed the 'Example'
and result's :'+result);

startsWith
This method will return true if the given string starts with the prefix provided within the method.
Syntax
public Boolean startsWith(String prefix)
Example

String myString1 = 'This Is MyString EXAMPLE';
String prefix = 'This';
Boolean result = myString1.startsWith(prefix);
System.debug(' this may return true as our String starts with string 'This' and therefore the 
result's :'+result);

Apex – Arrays

Arrays in Apex are basically an equivalent as Lists in Apex. there’s no logical distinction between the Arrays and Lists as their internal arrangement and methods also are same but the array syntax is small traditional like Java.
Below is that the representation of an Array of Products −
Index 0 − HCL
Index 1 − H2SO4
Index 2 − NACL
Index 3 − H2O
Index 4 − N2
Index 5 − U296
Syntax

[] arrayOfProducts = new List();

Example
Suppose, we’ve to store the name of our Products – we will use the Array where in, we’ll store the merchandise Names as shown below. you’ll access the actual Product by specifying the index.

//Defining array
String [] arrayOfProducts = new List();

//Adding elements in Array
arrayOfProducts.add('HCL');
arrayOfProducts.add('H2SO4');
arrayOfProducts.add('NACL');
arrayOfProducts.add('H2O');
arrayOfProducts.add('N2');
arrayOfProducts.add('U296');

for (Integer i = 0; i
//This loop will print all the weather in array
system.debug('Values In Array: '+arrayOfProducts[i]);
}

Accessing array element by using index

You can access any element in array by using the index as shown below −

//Accessing the element in array
//We would access the element at Index 3
System.debug('Value at Index 3 is :'+arrayOfProducts[3]);

Apex – Constants

As in the other programing language , Constants are the variables which don’t change their value once declared or assigned a worth .
In Apex, Constants are used once we want to define variables which should have constant value throughout the program execution. Apex constants are declared with the keyword ‘final’.
Example
Consider a CustomerOperationClass class and a continuing variable regularCustomerDiscount inside it −

public class CustomerOperationClass {
static final Double regularCustomerDiscount = 0.1;
static Double finalPrice = 0;

public static Double provideDiscount (Integer price) {
//calculate the discount
finalPrice = price - price * regularCustomerDiscount;
return finalPrice;
}
}

To see the Output of the above class, you’ve got to execute the subsequent code within the Developer Console Anonymous Window −

Double finalPrice = CustomerOperationClass.provideDiscount(100);
System.debug('finalPrice '+finalPrice);

So, this brings us to the end of blog. This Tecklearn ‘Understanding the String Manipulation , Arrays and Constants in Apex Programming Language’ blog helps you with commonly asked questions if you are looking out for a job in Salesforce. If you wish to learn Salesforce and build a career in Salesforce domain, then check out our interactive, Salesforce Certification Training: Admin 201 and App Builder, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Salesforce Certification Training: Admin 201 and App Builder

Salesforce Certification Training: Admin 201 and App Builder

About the Course

Salesforce Certification Training course will help you pass the Salesforce Administrator Exam (Admin 201) and the Salesforce App Builder (Dev 401) Exam. Concepts on Force.com Platform, AppExchange, SFDC Security Model, Service Cloud, Sales Cloud, Lightning App Builder, Salesforce Reports & Dashboard can be mastered in this Salesforce Training course. You can also configure the platform, manage users, find better ways to use the platform’s features, build applications with Salesforce Lightning, and more. Further, in this Salesforce certification training course, you will master App builder, Apex, Visualforce, etc.

Why Should you take Salesforce Admin 201 and App Builder Training?

• As per Indeed.com data, 200% global jump in Salesforce jobs since Jan 2016. Salesforce Certified Administrators earn an annual average salary of $87,000 but can go as high as $160,000 depending on their knowledge, skills, and experience.
• More than 200,000 companies worldwide use Salesforce platform. Salesforce leads the CRM market with 19.5 percent of market share – Forbes.
• The global CRM software market will reach US$40.26 billion in 2023, up from US$36.9 billion (2020) – Statista.

What you will Learn in this Course?

Salesforce Fundamentals
• Introduction to CRM concepts and Cloud computing
• Salesforce.com Overview and Fundamentals
• Understanding Salesforce Platform
Understanding Salesforce Platform
• Understanding Salesforce Terminologies and Introducing the force.com platform
• Understanding Salesforce Metadata and API
• Describe the capabilities of the core CRM objects in the Salesforce schema
• Identify common scenarios for extending an org using the AppExchange
• About Salesforce Certification
Introduction to Sales Cloud
• Sales Cloud
• Sales Process
• Sales Productivity Features
• Lead Management
• Lead auto response
• Lead assignment
• Web to lead
• Accounts and Contacts Management
• Opportunities
• Campaign Management
Security Model, User Management and Its Features
• Security Model Mind Map
• System Level or Org Level Security
• User Administration and Troubleshooting
• Permission Sets
• Profile Management
• User Actions
• Assigning Permission
• Session settings
• Activations
• Page layout assignment
• Tab setting
• Field level security
Object, Record and Field Level Features
• Custom Object
• Custom Field
• Data Types
• Relationship among Objects
• Working with App and Tabs
Data Handling and Processing
• Data Import and Export with Salesforce
• Insert, Update and Delete Data with Salesforce
• Export Data with UI
• Export Data using Data Loader Tool
Deployment
• SandBox
• Moving Data from SB to Production – Deployment
• Types of SandBox
• Change Sets
• Types of Change Sets
Application Cycle
• Milestones
• Sandboxes
• Change Sets
• Packages
Reports and Dashboards
Declarative Implementation in Salesforce
Salesforce Development and Apex Programming
• Apex Programming
• Apex Classes
• Apex Settings
• SOQL – Salesforce Object Query Language
• DML Commands
• Apex Class in Detail
• Apex Triggers
• Apex Testing
• Access Specifier in Salesforce
• Testing
Lightning in Salesforce
• Lightning Components
• Lightning Component Capabilities
• Lightning Components vs. Visualforce
Visual Force in Salesforce
• Standard Visualforce controller and controller extensions,
• Visualforce Page
• Understanding the MVC Pattern
• Tools for Visualforce Development
• Visual Force Components
WorkFlows in Salesforce
• Work Flows in Salesforce
• Types of Work Flows
• Work Flows Rules
About Preparation of Salesforce 201 and App Builder Certification exams

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Understanding the String Manipulation, Arrays and Constants in Apex programming language"

Leave a Message

Your email address will not be published. Required fields are marked *