Skip to main content

Mastering String Manipulation in Java: A Guide to String Functions with Code Examples

Strings and String Functions in Java

What is a String in Java?

In Java, a string is a sequence of characters. The String class is used to represent strings in Java. Strings in Java are immutable, which means that once a string is created, it cannot be changed. Instead, any operation that appears to modify a string actually creates a new string. For example, the following code creates two separate string objects:

String str1 = "Hello";
String str2 = str1 + " world"; // creates a new string object
System.out.println(str1); // "Hello"
System.out.println(str2); // "Hello world"

String Functions in Java

The String class provides many useful functions for working with strings in Java. Here are some of the most commonly used functions:

  • length(): returns the length of the string
  • charAt(int index): returns the character at the specified index
  • indexOf(String str): returns the index of the first occurrence of the specified substring
  • substring(int beginIndex, int endIndex): returns a substring of the original string
  • toUpperCase(): returns a new string with all characters converted to uppercase
  • toLowerCase(): returns a new string with all characters converted to lowercase
  • replace(char oldChar, char newChar): returns a new string with all occurrences of the old character replaced by the new character
  • replaceAll(String regex, String replacement): returns a new string with all occurrences of the regular expression replaced by the replacement string

Here's an example that demonstrates some of these functions:

String str = "Hello, world!";
System.out.println("Length: " + str.length()); // 13
System.out.println("Character at index 1: " + str.charAt(1)); // 'e'
System.out.println("Index of first 'o': " + str.indexOf("o")); // 4
System.out.println("Substring from index 7 to end: " + str.substring(7)); // "world!"
System.out.println("Uppercase: " + str.toUpperCase()); // "HELLO, WORLD!"
System.out.println("Lowercase: " + str.toLowerCase()); // "hello, world!"
System.out.println("Replace 'o' with 'x': " + str.replace('o', 'x')); //
		System.out.println("Replace all 'l' with 'x': " + str.replaceAll("l", "x")); // "Hexxo, worxd!"
		System.out.println("Replace all vowels with '*': " + str.replaceAll("[aeiou]", "*")); // "H*ll*, w*rld!"

The first call to the replaceAll() function replaces all occurrences of the character 'o' with 'x'. The second call replaces all occurrences of the character 'l' with 'x'. Note that the regular expression in the third call matches any vowel, and replaces it with the character '*'. Regular expressions are a powerful tool for working with strings in Java.

Conclusion

Strings are a fundamental data type in Java, and the String class provides many useful functions for working with strings. By understanding how to use these functions, you can manipulate strings in a variety of ways to suit your needs.

Comments

Popular posts from this blog

a simple example for jdbc PreparedStatement

a simple example for PreparedStatement package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; Statement stmt=null; ResultSet rs=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql="select * fr...

Server-Side Pagination with React-Table and Spring Boot JPA with H2 Database

Pagination is a common technique used to split large amounts of data into smaller, more manageable chunks. With server-side pagination, data is retrieved from the server in smaller batches, reducing the amount of data transferred over the network and improving application performance. React-Table provides a wide range of built-in features such as sorting, filtering, pagination, row selection, and column resizing. These features can be easily configured and customized to fit specific requirements. For example, you can customize the sorting behavior to handle multiple sorting criteria, or you can add custom filters to the table to handle complex data filtering scenarios. Additionally, React-Table provides a flexible API that allows developers to extend its functionality with custom hooks, plugins, and components. This means that you can easily add custom functionality to the table, such as exporting data to CSV or integrating with external data sources. In terms of styl...