0% found this document useful (0 votes)
1 views3 pages

String Handling in Java

String handling in Java involves creating, modifying, and comparing strings, which are sequences of characters used in various applications. Key operations include concatenation, character extraction, and validation, with special methods for string comparison and manipulation. Additionally, StringBuffer allows for mutable string operations, making it efficient for frequent updates.

Uploaded by

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

String Handling in Java

String handling in Java involves creating, modifying, and comparing strings, which are sequences of characters used in various applications. Key operations include concatenation, character extraction, and validation, with special methods for string comparison and manipulation. Additionally, StringBuffer allows for mutable string operations, making it efficient for frequent updates.

Uploaded by

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

What Is String Handling?

A String in Java is a sequence of characters enclosed in double quotes.


Almost every Java application works with text (names, passwords, Where Strings Show Up

String Handling in Java emails, addresses, messages, and reports) which makes string
handling one of the most essential skills for any developer. Names & Addresses

A detailed walkthrough of constructors, operations, comparison, Passwords


modification, and String Buffer
Email Addresses
String name = "John";

[Link](name); Messages & Reports


// Output: John

Creating Strings: Constructors String Length


The length() method returns the number of characters in a

String Literal

String s = "Java";
new String()

String s = new
From Char Array

char[] a = {'H','i'};
From Byte Array

byte[] b = {65,66};
string. It's frequently used for validation — for example, checking
that a password meets a minimum length requirement. 7 Characters in
"Java123"

String("Hi"); String s = new String s = new


String(a); String(b);

String password = "Java123";

if ([Link]() >= 8) {

9
Most common way to create a Creates a new String object Builds a string from characters. ASCII values become characters.
string. explicitly. [Link]("Strong Password"); Characters in
} else { "Java12345" — valid!
[Link]("Weak Password");
}
// Output: Weak Password

3 4
Special String Operations (Part 1) Special String Operations (Part 2)

concat() contains() startsWith() endsWith()


[Link](" " + Joins two strings together — e.g. building a student's full name.
lastName) [Link]("@") — verifies an email has [Link]("https") — confirms a site [Link](".pdf") — checks an uploaded
an "@" symbol. uses HTTPS. file type.

equals()
[Link](actual) Checks whether two strings are exactly equal — e.g. verifying a login username.
trim() toUpperCase() toLowerCase()

Removes extra spaces users enter in "Bangalore" → "BANGALORE" for consistent "BANGALORE" → "bangalore" for normalized
registration forms. display. comparisons.
equalsIgnoreCase
()
"JAVA".equalsIgnoreCase(" Compares strings while ignoring case — e.g. matching city names typed by users.
java")

5 6

Character Extraction Comparing & Modifying Strings


Java lets you pull individual characters or arrays of characters out of a string using charAt(),
toCharArray(), and getChars(). String Comparison Modifying Strings

compareTo() Strings are immutable


charAt(2) → 'v' toCharArray() Compares strings alphabetically and returns a negative, Once created, a String's value can't change. Methods like
Converts a string into a character array — useful for validating each positive, or zero value. replace() and substring() return a new String object.
J a v a character while checking a password.

"Apple".compareTo("Banana") String email = "student@[Link]";


0 1 2 3 // negative (Apple before Banana)
char[] letters = "Hello".toCharArray(); [Link](
// H e l l o [Link]("@") + 1);
"Java".compareTo("Java") // Output: [Link]
// 0 (equal strings)
String s = "Java";
[Link]([Link](2));
// Output: v getChars()
[Link](0,5,arr,0) copies "Progr" from "Programming" into another
Used to sort names alphabetically: Amit, John, Rahul, Zara. replace("Java","Python") → "Python Programming"
array.

7 8
StringBuffer: Mutable Strings String vs. StringBuffer
StringBuffer creates mutable strings. Unlike String, changes modify the
append()
same object — making it efficient for repeated updates, like a chat app Feature String StringBuffer
Adds text to the end
appending characters as a user types.

insert() Nature Immutable (cannot change) Mutable (can be changed)

Inserts text at a position


StringBuffer message = new StringBuffer();
Memory New object per modification Modifies the same object
[Link]("H"); replace()
[Link]("e");
[Link]("l"); Replaces a range of characters Performance Slower for frequent edits Faster for frequent edits
[Link]("l");
[Link]("o"); delete()
Thread Safety Safe because immutable Thread-safe (synchronized)
[Link](message); Removes a range of characters
// Output: Hello
Best Use Fixed text: names, emails Changing text: chats, editors
reverse()
Reverses the character order

9 10

Thank You

You might also like