0% found this document useful (0 votes)
9 views

String Functions

Uploaded by

kiransekar95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

String Functions

Uploaded by

kiransekar95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

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 = str1.length();
System.out.println("The length of str1 is: " + length1);

// Getting the length of str2


int length2 = str2.length();
System.out.println("The length of str2 is: " + length2);

// Getting the length of str3


int length3 = str3.length();
System.out.println("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 + "!";
System.out.println("Using + operator: " + result1);

// Using the concat method


String result2 = str1.concat(", ").concat(str2).concat("!");
System.out.println("Using concat method: " + result2);

// Using StringBuilder
StringBuilder sb = new StringBuilder();
sb.append(str1);
sb.append(", ");
sb.append(str2);
sb.append("!");
String result3 = sb.toString();
System.out.println("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=name.charAt(4);//returns the char value at the 4th index
System.out.println(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 (str1.equals(str2)) {
System.out.println("str1 and str2 are equal");
} else {
System.out.println("str1 and str2 are not equal");
}

// Comparing str1 and str3


if (str1.equals(str3)) {
System.out.println("str1 and str3 are equal");
} else {
System.out.println("str1 and str3 are not equal");
}

// Comparing str1 and str3 ignoring case


if (str1.equalsIgnoreCase(str3)) {
System.out.println("str1 and str3 are equal ignoring case");
} else {
System.out.println("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 = str1.compareTo(str2);
if (result1 < 0) {
System.out.println("str1 is less than str2");
} else if (result1 > 0) {
System.out.println("str1 is greater than str2");
} else {
System.out.println("str1 is equal to str2");
}

// Comparing str1 and str3


int result2 = str1.compareTo(str3);
if (result2 < 0) {
System.out.println("str1 is less than str3");
} else if (result2 > 0) {
System.out.println("str1 is greater than str3");
} else {
System.out.println("str1 is equal to str3");
}

// Comparing str1 and str4


int result3 = str1.compareTo(str4);
if (result3 < 0) {
System.out.println("str1 is less than str4");
} else if (result3 > 0) {
System.out.println("str1 is greater than str4");
} else {
System.out.println("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 = str.indexOf('o');
System.out.println("The index of the first 'o' is: " + index1);

// Find the index of the first occurrence of "World"


int index2 = str.indexOf("World");
System.out.println("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 = str.indexOf('o', 5);
System.out.println("The index of the first 'o' after index 5 is: " +
index3);

// Find the index of a substring that doesn't exist


int index4 = str.indexOf("Java");
System.out.println("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 = str.lastIndexOf('o');
System.out.println("The last index of 'o' is: " + index1);

// Find the last index of the substring "Hello"


int index2 = str.lastIndexOf("Hello");
System.out.println("The last index of \"Hello\" is: " + index2);

// Find the last index of the character 'o' before index 20


int index3 = str.lastIndexOf('o', 20);
System.out.println("The last index of 'o' before index 20 is: " + index3);

// Find the last index of a substring that doesn't exist


int index4 = str.lastIndexOf("Java");
System.out.println("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 = str.substring(7);
System.out.println("Substring from index 7 to the end: " + substr1);

// Extract substring from index 0 to 5 (excluding index 5)


String substr2 = str.substring(0, 5);
System.out.println("Substring from index 0 to 5: " + substr2);

// Extract substring from index 7 to 12 (excluding index 12)


String substr3 = str.substring(7, 12);
System.out.println("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 = str1.concat(", ").concat(str2).concat("!");
System.out.println("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 = str.replace('l', 'x');
System.out.println("After replacing 'l' with 'x': " + result1);

// Replace the substring "World" with "Java"


String result2 = str.replace("World", "Java");
System.out.println("After replacing \"World\" with \"Java\": " + result2);

// Replace all occurrences of 'o' with 'a'


String result3 = str.replace('o', 'a');
System.out.println("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 = str1.trim();
System.out.println("Original string 1: '" + str1 + "'");
System.out.println("Trimmed string 1: '" + result1 + "'");

String result2 = str2.trim();


System.out.println("Original string 2: '" + str2 + "'");
System.out.println("Trimmed string 2: '" + result2 + "'");

String result3 = str3.trim();


System.out.println("Original string 3: '" + str3 + "'");
System.out.println("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 = String.join(", ", words);
System.out.println("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


stringBuffer.append(" World");

// Insert text at a specific position


stringBuffer.insert(6, ", Java");

// Replace text
stringBuffer.replace(12, 16, "Program");

// Delete text
stringBuffer.delete(0, 6);

// Reverse the string


stringBuffer.reverse();

// Convert StringBuffer to String and print


String result = stringBuffer.toString();
System.out.println("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


stringBuilder.append("Hello");
stringBuilder.append(" ");
stringBuilder.append("World");
stringBuilder.append("!");

// Convert StringBuilder to String and print


String result = stringBuilder.toString();
System.out.println("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


stringBuilder.insert(5, ", Java");

// Insert a character at index 0


stringBuilder.insert(0, "Hey ");

// Insert an integer at index 18


stringBuilder.insert(18, 2024);

// Convert StringBuilder to String and print


String result = stringBuilder.toString();
System.out.println("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


reversedString.reverse();

// Convert StringBuilder back to String and print


String result = reversedString.toString();
System.out.println("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)


stringBuilder.delete(5, 12);

// Convert StringBuilder to String and print


String result = stringBuilder.toString();
System.out.println("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"


stringBuffer.append(" World");
System.out.println("After append: " + stringBuffer);

// Insert "Java" at index 6


stringBuffer.insert(6, " Java");
System.out.println("After insert: " + stringBuffer);

// Reverse the string


stringBuffer.reverse();
System.out.println("After reverse: " + stringBuffer);

// Delete characters from index 5 to index 10 (excluding index 10)


stringBuffer.delete(5, 10);
System.out.println("After delete: " + stringBuffer);

// Replace " Java" with " programming"


stringBuffer.replace(6, 11, " programming");
System.out.println("After replace: " + stringBuffer);

// Get a substring from index 6 to the end


String substring = stringBuffer.substring(6);
System.out.println("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-----------------------------------------------------------------------

You might also like