Java String Methods

String Methods in Java

Concatenation: The + operator is used for string concatenation, which means combining two or more strings together.

Example
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;

Length:

You can find the length of a string using the length() method.

Example
String text = "Hello, World!";
int length = text.length(); // length is 13

Accessing Characters:

You can access individual characters in a string using the charAt() method.

Example
String text = "Hello, World!";
char firstChar = text.charAt(0); // firstChar is 'H'

Comparing Strings:

To compare two strings for equality, use the equals() method.

Example
String str1 = "Java";
String str2 = "java";
boolean isEqual = str1.equals(str2); // isEqual is false

Substring Operations

Extracting Substrings:

You can extract a portion of a string using the substring() method.

Example
String text = "Hello, World!";
String subText = text.substring(0, 5); // subText is "Hello"

Finding Substrings:

To find the position of a substring within a string, use the indexOf() method.

Example
String text = "Hello, World!";
int index = text.indexOf("World"); // index is 7

Modifying Strings

Concatenating Strings:

To concatenate a string to an existing one, you can use the concat() method or the += operator.

Example
String greeting = "Hello";
greeting = greeting.concat(", World!"); // greeting is "Hello, World!"

Replacing Characters:

To replace characters within a string, use the replace() method.

Example
String message = "I like Java";
String newMessage = message.replace("Java", "Python"); // newMessage is "I like Python"

Removing Whitespace:

To remove leading and trailing whitespace, use the trim() method.

Example
String input = "   Java Programming   ";
String trimmed = input.trim(); // trimmed is "Java Programming"

Changing Case:

You can change the case of a string using methods like toUpperCase() and toLowerCase().

Example
String text = "Hello, World!";
String uppercaseText = text.toUpperCase(); // uppercaseText is "HELLO, WORLD!"

Searching within Strings

  • indexOf() and lastIndexOf(): These methods are used to find the index of a character or substring within a string.
  • startsWith() and endsWith(): To check if a string starts or ends with a specific substring, use these methods.
  • contains(): You can determine if a string contains a specific substring using the contains() method.

Special Characters and Escape Sequences

  • Handling Escape Sequences: Java supports escape sequences like \n (newline) and \t (tab). Use them when dealing with special characters.
  • Splitting Strings: You can split a string into an array of substrings using the split() method.
Example
String sentence = "Java is a powerful language";
String[] words = sentence.split(" "); // words is ["Java", "is", "a", "powerful", "language"]

Mastering these essential string methods in Java is crucial for effective text manipulation and data processing in your Java applications. Whether you're extracting substrings, modifying content, searching for specific characters, or handling escape sequences, a good understanding of these methods will greatly enhance your programming capabilities.

Contact Us

Name
Email
Mobile No:
subject:
Message: