Substring Functions
String Functions are the text functions which are predefined to make developer job easy. There are different types of substring functions, these functions return a substring in a string. Some of them are discussed below:
1. Left() Function
The Left() function, returns the substring from the left side of the string. The function takes two mandatory arguments i.e. input_string and number.
Syntax of the function: Left(input_string, number)
- input_string: It is the first argument of the Left() function. It is the input string, for which the left substring has to be returned.
- number: It is the second argument of the Left() function. It tells the number of characters to be returned from the left start of the string.
For example, consider a string “Coding”, a left substring of length two can be returned using Left(“Coding”, 2). The final output of the str is “Co”.
2. Right() Function
The Right() function, returns the substring from the right side of the string. The function takes two mandatory arguments i.e. input_string and number.
Syntax of the function: Right(input_string, number)
- input_string: It is the first argument of the Right() function. It is the input string, for which the right substring has to be returned.
- number: It is the second argument of the Right() function. It tells the number of characters to be returned from the right start of the string.
For example, consider a string “Coding”, a right substring of length two can be returned using Right(“Coding”, 2). The final output of the str is “ng”.
3. Mid() Function
The Mid() function, returns a substring of any size a user wants. The function takes three mandatory arguments i.e. input_string, number1, and number2.
Syntax of the function: Mid(input_string, number1, number2)
- input_string: It is the first argument of the Mid() function. It is the input string, for which the substring has to be returned.
- number1: It is the second argument of the Mid() function. It tells the starting index of the substring.
- number2: It is the third argument of the Mid() function. It tells the number of characters of the substring.
For example, consider a string “Coding”, and we need to return a substring “odi”. This can be achieved using the Mid(“Coding”, 2, 3) function.
Note: 1-based indexing of string has been considered in the Mid() function.
VBA Strings in Excel
In Excel’s Visual Basic for Applications(VBA), strings are pivotal in handling and manipulating text-based data. Strings serve as a fundamental data type used to store a sequence of characters, enabling the representation of textual information, numbers, symbols, and more. Understanding how VBA handles strings is essential for crafting powerful macros and automating tasks within Excel.
Here, we’ll learn about what is a VBA String Function and what are the different types of VBA String Functions.