Strings in Java

Introduction:


Strings in Java are immutable objects. This means that once a string object has been created, its contents cannot be modified. Strings are created with the String class, which offers several constructors and methods to manipulate strings.

In Java, a String is an object that represents a sequence of characters. String literals are created by enclosing a sequence of characters between a pair of double quotes.

String Class in Java:

The String class provides a series of methods for manipulating strings, including the ability to split strings, trim characters from the start or end, and replace certain characters with others. Some of the most important methods of the String class include:

Constructors:

  • String(): Creates a new string object with no characters.
  • String(char[] array): Creates a new string object by copying the contents of the specified array of characters.
  • String(String original): Creates a new string object by copying the contents of the specified string.
  • String(String original, int start, int end): Creates a new string object by copying the characters from start to end-1 of the specified string.
  • String(StringBuffer buffer): Creates a new string object by copying the contents of the specified string buffer.

Methods:

  • length(): Returns the length of the string.
  • charAt(int index): Returns the character at the specified index.
  • substring(int start): Returns a new string object containing the substring from start to the end of the string.
  • substring(int start, int end): Returns a new string object containing the substring from start to end-1 of the string.
  • equals(Object object): Returns true if the specified object is equal to the string.
  • replace(char oldChar, char newChar): Returns a new string object with all occurrences of oldChar replaced by newChar.
  • toLowerCase(): Returns a new string object with all characters converted to lowercase.
  • trim(): Returns a new string object with whitespace (space, tab, newline, etc.) removed from the beginning and end of the string.
  • toString(): Returns the string representation of the string object.
// Create a new String object
String str = new String("My string");

// Get the length of the String
int len = str.length();

// Get the character at the specified index
char c = str.charAt(0);

// Create a substring of the original String
String substring = str.substring(2);

// Check if the String is equal to another
boolean isEqual = str.equals("My string");

// Replace a character in the String
String newString = str.replace("y", "x");

// Convert the String to lowercase
String lowercase = str.toLowerCase();

// Remove whitespace from the beginning and end
String trimmed = str.trim();

// Convert the String to a String representation
String strRepresentation = str.toString();

These methods allow you to manipulate strings in many different ways. You can use them in order to manipulate and process strings in your Java code.

, ,

Related posts

Advantages of Java:

Introduction:

Java is a popular and versatile programming language that has been around since 1995....

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site