0% found this document useful (0 votes)
108 views4 pages

Java String Methods Explained

The document provides a detailed overview of Java String methods, covering topics such as string creation, length, character access, substring, comparison, searching, case conversion, trimming, replacing, splitting, joining, value conversion, checking for emptiness, interning, and formatting. Each method is explained with examples to illustrate its usage. This serves as a comprehensive guide for understanding and utilizing string operations in Java.

Uploaded by

Arunprakash
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)
108 views4 pages

Java String Methods Explained

The document provides a detailed overview of Java String methods, covering topics such as string creation, length, character access, substring, comparison, searching, case conversion, trimming, replacing, splitting, joining, value conversion, checking for emptiness, interning, and formatting. Each method is explained with examples to illustrate its usage. This serves as a comprehensive guide for understanding and utilizing string operations in Java.

Uploaded by

Arunprakash
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

Java String Methods - Detailed Overview

1. String Creation
• String s1 = "Hello"; // String literal
• String s2 = new String("Hello"); // Using new keyword

2. String Length
• length() → Returns number of characters in the string.

String str = "Java";


[Link]([Link]()); // 4

3. Character Access
• charAt(int index) → Returns character at given index.

String s = "Java";
[Link]([Link](2)); // v

4. Substring
• substring(int beginIndex)
• substring(int beginIndex, int endIndex)

String s = "HelloWorld";
[Link]([Link](5)); // World
[Link]([Link](0, 5)); // Hello

5. String Comparison
• equals(String str)
• equalsIgnoreCase(String str)

1
• compareTo(String str)
• compareToIgnoreCase(String str)

String a = "Java", b = "java";


[Link]([Link](b)); // false
[Link]([Link](b));// true
[Link]([Link](b)); // -32

6. Searching in Strings
• contains(CharSequence s)
• startsWith(String prefix)
• endsWith(String suffix)
• indexOf(String s)
• lastIndexOf(String s)

String s = "Programming";
[Link]([Link]("gram")); // true
[Link]([Link]("Pro")); // true
[Link]([Link]("ing")); // true
[Link]([Link]("g")); // 3
[Link]([Link]("g")); // 10

7. Case Conversion
• toUpperCase()
• toLowerCase()

String s = "Java";
[Link]([Link]()); // JAVA
[Link]([Link]()); // java

8. Trim & Strip


• trim() → Removes leading and trailing spaces.
• strip() → Removes all Unicode whitespaces.

2
String s = " Hello World ";
[Link]([Link]()); // "Hello World"

9. Replace
• replace(char oldChar, char newChar)
• replace(CharSequence target, CharSequence replacement)
• replaceAll(String regex, String replacement)
• replaceFirst(String regex, String replacement)

String s = "Java Programming";


[Link]([Link]("Java", "C++")); // C++ Programming
[Link]([Link]("a", "@")); // J@v@ Progr@mming

10. Splitting & Joining


• split(String regex)
• join(CharSequence delimiter, elements...)

String s = "a,b,c";
String[] arr = [Link](",");
for(String x : arr) [Link](x); // a b c

String joined = [Link]("-", "Java", "Python", "C++");


[Link](joined); // Java-Python-C++

11. Value Conversion


• valueOf(int/float/boolean/char/…)
• toCharArray()

int num = 100;


String s = [Link](num);
[Link](s + 50); // 10050

3
12. Checking Empty or Blank
• isEmpty()
• isBlank() (Java 11+)

String s1 = "";
[Link]([Link]()); // true
[Link](" ".isBlank()); // true

13. Interning
• intern() → Stores string in string pool.

String s1 = new String("Hello");


String s2 = "Hello";
[Link](s1 == s2); // false
[Link]([Link]() == s2); // true

14. Formatting
• format(String format, Object... args)

String result = [Link]("Name: %s, Age: %d", "Arun", 25);


[Link](result); // Name: Arun, Age: 25

Common questions

Powered by AI

The 'split' method breaks a string into an array of substrings based on a specified delimiter, enabling efficient parsing and data manipulation. Conversely, the 'join' method reassembles strings from arrays into a single string with a chosen delimiter . Together, they provide powerful tools for comprehensive string manipulation: 'split' decomposes complex strings for data processing, while 'join' creates cohesive outputs from individual components, as seen in 'String.join("-", "Java", "Python", "C++")' forming 'Java-Python-C++' .

The 'equals(String str)' method checks if two strings have the identical sequence of characters and returns a boolean result. It is case-sensitive, so 'Java'.equals('java') results in false . Conversely, the 'compareTo(String str)' method compares two strings lexicographically and returns an integer value: zero if they are equal, a positive number if the calling string is lexicographically greater, and a negative number if it is less . Therefore, 'Java'.compareTo('java') returns -32 due to ASCII value differences .

String interning in Java optimizes memory by storing strings with the same content in the string pool, avoiding duplicate instances. Using 'intern()', a string's reference is stored in the pool if not already present. This reduces memory footprint when many identical strings are created since the JVM can reference the same object, improving performance for applications with extensive string operations . However, interning should be used judiciously, as excessive interning can lead to high memory usage if applied unnecessarily .

The 'trim()' method removes only the leading and trailing spaces (ASCII whitespace) from a string . In contrast, 'strip()' removes all kinds of Unicode whitespace characters, which means it is more comprehensive in handling whitespace defined by Unicode standards . The distinction is particularly useful in dealing with internationalized text where Unicode whitespace can appear.

The output of the code snippet System.out.println(s.replaceAll("a", "@")) will be 'J@v@ Progr@mming' . The 'replaceAll' method is used to replace occurrences of a specified regular expression with the given replacement substring. In this case, every 'a' in the string 'Java Programming' is replaced with '@' .

The 'format' method provides a structured way to create dynamic messages by embedding variable placeholders that are replaced with specified arguments, improving readability and maintainability. It supports various data types and formatting settings . Compared to simple concatenation, 'format' offers enhanced flexibility and clarity, especially in scenarios requiring complex string outputs with mixed data types, as seen in 'String.format("Name: %s, Age: %d", "Arun", 25)' . This method reduces errors in code modifications and increases scalability for internationalization .

'isEmpty()' checks if a string's length is zero, being useful for basic input validation to catch completely empty inputs . Conversely, 'isBlank()' checks if a string is empty or contains only whitespace, offering more robust validation especially for text input forms where users might accidentally enter spaces . Implementing both methods ensures that user inputs are effectively validated, preventing erroneous or malicious data entry in Java applications.

Using the 'substring' method without validating indices can lead to StringIndexOutOfBoundsException. For example, if 'substring(5, 10)' is called on a string of length 8, it will raise an error because the end index is beyond the string length . Proper checks for index bounds before usage can prevent runtime errors.

The 'indexOf(String s)' method returns the index of the first occurrence of the specified substring, returning -1 if the substring is not found. It is useful for finding the first appearance of a keyword or character from the beginning of a string . For instance, in 'Programming', indexOf("g") returns 3 . The 'lastIndexOf(String s)' method returns the index of the last occurrence of the specified substring, also returning -1 if not found . It is useful when searching for a substring that appears multiple times, and you need the last occurrence, like finding the last occurrence of 'g' in 'Programming', which returns 10 .

Using 'toUpperCase()' and 'toLowerCase()' without considering locale can lead to incorrect behavior in locale-sensitive applications. For instance, the Turkish alphabet includes dotted and dotless 'I', which convert differently than in English. Without specifying locale, 'toUpperCase()' might not convert 'i' to 'İ', impacting functionality like user input normalization and sorting . To avoid these pitfalls, specifying a locale parameter in the methods, such as 'toUpperCase(Locale.forLanguageTag("tr"))', ensures correct behavior across different languages .

You might also like