STRING FUNCTIONS
STRING LENGTH -
INPUT :
public class LengthExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "";
String str3 = "Java Programming";
// Getting the length of str1
int length1 = [Link]();
[Link]("The length of str1 is: " + length1);
// Getting the length of str2
int length2 = [Link]();
[Link]("The length of str2 is: " + length2);
// Getting the length of str3
int length3 = [Link]();
[Link]("The length of str3 is: " + length3);
}
}
OUTPUT :
The length of str1 is: 13
The length of str2 is: 0
The length of str3 is: 16
STRING CONCATENATION :
INPUT :
public class ConcatenationExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
// Using the + operator
String result1 = str1 + ", " + str2 + "!";
[Link]("Using + operator: " + result1);
// Using the concat method
String result2 = [Link](", ").concat(str2).concat("!");
[Link]("Using concat method: " + result2);
// Using StringBuilder
StringBuilder sb = new StringBuilder();
[Link](str1);
[Link](", ");
[Link](str2);
[Link]("!");
String result3 = [Link]();
[Link]("Using StringBuilder: " + result3);
}
}
OUTPUT :
Using + operator: Hello, World!
Using concat method: Hello, World!
Using StringBuilder: Hello, World!
CHARACTER EXTRACTION :
CharAt :
INPUT :
public class CharAtExample{
public static void main(String args[]){
String name="javatpoint";
char ch=[Link](4);//returns the char value at the 4th index
[Link](ch);
}}
OUTPUT :
GETCHAR - ?
STRING COMPARISION -
EQUALS AND EQUALSISIGNORECASE :
INPUT :
public class EqualsExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = "hello";
// Comparing str1 and str2
if ([Link](str2)) {
[Link]("str1 and str2 are equal");
} else {
[Link]("str1 and str2 are not equal");
}
// Comparing str1 and str3
if ([Link](str3)) {
[Link]("str1 and str3 are equal");
} else {
[Link]("str1 and str3 are not equal");
}
// Comparing str1 and str3 ignoring case
if ([Link](str3)) {
[Link]("str1 and str3 are equal ignoring case");
} else {
[Link]("str1 and str3 are not equal even ignoring case");
}
}
}
OUTPUT :
str1 and str2 are equal
str1 and str3 are not equal
str1 and str3 are equal ignoring case
COMPARETO :
INPUT :
public class CompareToExample {
public static void main(String[] args) {
String str1 = "Apple";
String str2 = "Banana";
String str3 = "Apple";
String str4 = "apple";
// Comparing str1 and str2
int result1 = [Link](str2);
if (result1 < 0) {
[Link]("str1 is less than str2");
} else if (result1 > 0) {
[Link]("str1 is greater than str2");
} else {
[Link]("str1 is equal to str2");
}
// Comparing str1 and str3
int result2 = [Link](str3);
if (result2 < 0) {
[Link]("str1 is less than str3");
} else if (result2 > 0) {
[Link]("str1 is greater than str3");
} else {
[Link]("str1 is equal to str3");
}
// Comparing str1 and str4
int result3 = [Link](str4);
if (result3 < 0) {
[Link]("str1 is less than str4");
} else if (result3 > 0) {
[Link]("str1 is greater than str4");
} else {
[Link]("str1 is equal to str4");
}
}
}
OUTPUT :
str1 is less than str2
str1 is equal to str3
str1 is less than str4
SEARCHING STRINGS -
INDEXOF :
INPUT :
public class IndexOfExample {
public static void main(String[] args) {
String str = "Hello, World!";
// Find the index of the first occurrence of 'o'
int index1 = [Link]('o');
[Link]("The index of the first 'o' is: " + index1);
// Find the index of the first occurrence of "World"
int index2 = [Link]("World");
[Link]("The index of the first occurrence of \"World\" is: " +
index2);
// Find the index of the first occurrence of 'o' starting from index 5
int index3 = [Link]('o', 5);
[Link]("The index of the first 'o' after index 5 is: " +
index3);
// Find the index of a substring that doesn't exist
int index4 = [Link]("Java");
[Link]("The index of the first occurrence of \"Java\" is: " +
index4);
}
}
OUTPUT :
The index of the first 'o' is: 4
The index of the first occurrence of "World" is: 7
The index of the first 'o' after index 5 is: 8
The index of the first occurrence of "Java" is: -1
LASTINDEXOF :
INPUT :
public class LastIndexOfExample {
public static void main(String[] args) {
String str = "Hello, World! Hello, Universe!";
// Find the last index of the character 'o'
int index1 = [Link]('o');
[Link]("The last index of 'o' is: " + index1);
// Find the last index of the substring "Hello"
int index2 = [Link]("Hello");
[Link]("The last index of \"Hello\" is: " + index2);
// Find the last index of the character 'o' before index 20
int index3 = [Link]('o', 20);
[Link]("The last index of 'o' before index 20 is: " + index3);
// Find the last index of a substring that doesn't exist
int index4 = [Link]("Java");
[Link]("The last index of \"Java\" is: " + index4);
}
}
OUTPUT :
The last index of 'o' is: 20
The last index of "Hello" is: 14
The last index of 'o' before index 20 is: 8
The last index of "Java" is: -1
MODIFYING A STRING :
1. SUBSTRING -
INPUT:
public class SubstringExample {
public static void main(String[] args) {
String str = "Hello, World!";
// Extract substring from index 7 to the end
String substr1 = [Link](7);
[Link]("Substring from index 7 to the end: " + substr1);
// Extract substring from index 0 to 5 (excluding index 5)
String substr2 = [Link](0, 5);
[Link]("Substring from index 0 to 5: " + substr2);
// Extract substring from index 7 to 12 (excluding index 12)
String substr3 = [Link](7, 12);
[Link]("Substring from index 7 to 12: " + substr3);
}
}
OUTPUT :
Substring from index 7 to the end: World!
Substring from index 0 to 5: Hello
Substring from index 7 to 12: World
2. CONCAT -
INPUT :
public class ConcatExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
// Using the concat method to join str1 and str2
String result = [Link](", ").concat(str2).concat("!");
[Link]("Concatenated string: " + result);
}
}
OUTPUT :
Concatenated string: Hello, World!
3. REPLACE -
INPUT :
public class ReplaceExample {
public static void main(String[] args) {
String str = "Hello, World!";
// Replace all occurrences of 'l' with 'x'
String result1 = [Link]('l', 'x');
[Link]("After replacing 'l' with 'x': " + result1);
// Replace the substring "World" with "Java"
String result2 = [Link]("World", "Java");
[Link]("After replacing \"World\" with \"Java\": " + result2);
// Replace all occurrences of 'o' with 'a'
String result3 = [Link]('o', 'a');
[Link]("After replacing 'o' with 'a': " + result3);
}
}
OUTPUT :
After replacing 'l' with 'x': Hexxo, Worxd!
After replacing "World" with "Java": Hello, Java!
After replacing 'o' with 'a': Hella, Warld!
4. TRIM -
INPUT:
public class TrimExample {
public static void main(String[] args) {
String str1 = " Hello, World! ";
String str2 = "NoLeadingOrTrailingSpaces";
String str3 = " Leading and trailing spaces will be removed ";
// Using the trim method
String result1 = [Link]();
[Link]("Original string 1: '" + str1 + "'");
[Link]("Trimmed string 1: '" + result1 + "'");
String result2 = [Link]();
[Link]("Original string 2: '" + str2 + "'");
[Link]("Trimmed string 2: '" + result2 + "'");
String result3 = [Link]();
[Link]("Original string 3: '" + str3 + "'");
[Link]("Trimmed string 3: '" + result3 + "'");
}
}
OUTPUT :
Original string 1: ' Hello, World! '
Trimmed string 1: 'Hello, World!'
Original string 2: 'NoLeadingOrTrailingSpaces'
Trimmed string 2: 'NoLeadingOrTrailingSpaces'
Original string 3: ' Leading and trailing spaces will be removed '
Trimmed string 3: 'Leading and trailing spaces will be removed'
JOINING A STRING :
STRING JOIN -
INPUT :
public class JoinExample {
public static void main(String[] args) {
String[] words = {"Hello", "World", "Java"};
// Join strings with a delimiter
String result = [Link](", ", words);
[Link]("Joined string: " + result);
}
}
OUTPUT :
Joined string: Hello, World, Java
STRING BUFFER -
INPUT :
public class StringBufferExample {
public static void main(String[] args) {
// Create a StringBuffer object
StringBuffer stringBuffer = new StringBuffer("Hello");
// Append text to the StringBuffer
[Link](" World");
// Insert text at a specific position
[Link](6, ", Java");
// Replace text
[Link](12, 16, "Program");
// Delete text
[Link](0, 6);
// Reverse the string
[Link]();
// Convert StringBuffer to String and print
String result = [Link]();
[Link]("Modified string: " + result);
}
}
OUTPUT :
Modified string: margorP avaJ
APPEND -
INPUT :
public class AppendExample {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
// Append strings and data
[Link]("Hello");
[Link](" ");
[Link]("World");
[Link]("!");
// Convert StringBuilder to String and print
String result = [Link]();
[Link]("Result: " + result);
}
}
OUTPUT :
Result: Hello World!
INSERT -
INPUT :
public class InsertExample {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("Hello World!");
// Insert a string at index 5
[Link](5, ", Java");
// Insert a character at index 0
[Link](0, "Hey ");
// Insert an integer at index 18
[Link](18, 2024);
// Convert StringBuilder to String and print
String result = [Link]();
[Link]("Result: " + result);
}
}
OUTPUT :
Result: Hey Hello, Java World!2024
REVERSE :
public class ReverseExample {
public static void main(String[] args) {
String originalString = "Hello, World!";
// Create a StringBuilder object with the original string
StringBuilder reversedString = new StringBuilder(originalString);
// Use the reverse() method to reverse the string
[Link]();
// Convert StringBuilder back to String and print
String result = [Link]();
[Link]("Reversed string: " + result);
}
}
OUTPUT :
Reversed string: !dlroW ,olleH
DELETE -
INPUT :
public class DeleteExample {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("Hello, World!");
// Delete characters from index 5 to index 12 (excluding index 12)
[Link](5, 12);
// Convert StringBuilder to String and print
String result = [Link]();
[Link]("Result: " + result);
}
}
OUTPUT :
Result: Hello!
A Java program that demonstrates performing various operations such as append,
insert, reverse, delete, replace, and substring using the 'StringBuffer' class:
INPUT :
public class StringBufferOperations {
public static void main(String[] args) {
// Create a StringBuffer object
StringBuffer stringBuffer = new StringBuffer("Hello");
// Append " World"
[Link](" World");
[Link]("After append: " + stringBuffer);
// Insert "Java" at index 6
[Link](6, " Java");
[Link]("After insert: " + stringBuffer);
// Reverse the string
[Link]();
[Link]("After reverse: " + stringBuffer);
// Delete characters from index 5 to index 10 (excluding index 10)
[Link](5, 10);
[Link]("After delete: " + stringBuffer);
// Replace " Java" with " programming"
[Link](6, 11, " programming");
[Link]("After replace: " + stringBuffer);
// Get a substring from index 6 to the end
String substring = [Link](6);
[Link]("Substring: " + substring);
}
}
iteration
sum -1 biggest
list set que n
map
OUTPUT :
After append: Hello World
After insert: Hello Java World
After reverse: dlroW avaJ olleH
After delete: dlroWvaJ olleH
After replace: dlroW programming olleH
Substring: programming olleH
-----------------------------------------------------------------
X-----------------------------------------------------------------------