
String Functions in Java
Strings are objects in Java, representing sequences of letters, numbers, symbols, and whitespace characters . They are immutable, meaning once created, their values cannot be changed and also strings are a fundamental data type used for representing sequences of characters. They're widely utilized for handling textual data in Java applications. Java provides a rich set of built-in functions, or methods, specifically designed for manipulating strings. These functions allow developers to perform various operations on strings efficiently.
String functions in Java provide various operations to manipulate strings, such as concatenation, slicing, formatting, and searching. Let's explore some common string functions:
str.toUpperCase(): Converts a string to uppercase.
str.toLowerCase(): Converts a string to lowercase.
str.length():returns the length of the string;
str.trim():Removes any leading and trailing whitespace characters from the string.
str.split():this function is used to split a string into an array of substrings based on a specified delimiter.
str.isEmpty(): Returns true if the length of the string is 0, otherwise false;
str.charAt(): Returns the character at the specified index;
str.startswith(): Checks if a string starts with a specified prefix.
str.endswith(): Checks if a string ends with a specified suffix.
str1.concat(String str2): Concatenates the str2 string to the end of str1 string.
Let's see some examples of using string functions in Python:
# length(): Returns the length of a string
Output:
11
# toUpperCase(): Converts a string to uppercase.
Output:
PBA INSTITUTE
Question.1 Write a program to accept a string and check Palindrome or not.
Example. MOM, MADAM, DAD etc.
OUTPUT:
Enter the String:
DAD
Palindrome.
Question.2 Write a program to accept a string and input a char value at the given index number.
Input: ROMA Enter the Index number=3 Output: M
Output:
Enter String:
ROMA
A
1. Write a program to reverse or mirror image the entered string.
Input: COMPUTER
Output: RETUPMOC
2. Write a program to enter a string and change the case of each alphabet of the string.
Input: Programming Beginner To Advanced Output: pROGRAMMING bEGINNER tO aDVANCED
3. Write a program to accept a string and find :
i) number of blank spaces in the string.
ii) number of words in the string iii) number of characters present in the string.
In Java, the 'String' class is fundamental for manipulating text. It provides numerous methods for operations like concatenation, comparison, substring extraction, and more. By utilizing these methods, developers can efficiently handle and manipulate textual data within Java programs. The versatility and robustness of 'String' functions make them indispensable for various applications, ranging from basic string manipulation to complex text processing tasks.