String:
In Java, a string is an object that represents a sequence of characters. The String
class in Java is used to create and manipulate strings. Strings in Java are immutable, which means once a string object is created, its value cannot be changed.
Here are some key concepts related to strings in Java:
- String Creation: Strings can be created using the
String
class constructor or by assigning a string literal to a variable.
javaString str1 = new String("Hello");
String str2 = "World";
- Concatenation: Strings can be concatenated using the
+
operator or theconcat()
method.
javaString result1 = str1 + str2;
String result2 = str1.concat(str2);
- String Length: The
length()
method returns the number of characters in a string.
javaint length = str1.length();
- Accessing Characters: Individual characters in a string can be accessed using the
charAt()
method.
javachar ch = str1.charAt(0); // Gets the character at index 0
- Substring: The
substring()
method is used to extract a portion of a string based on specified start and end indexes.
javaString substring = str1.substring(1, 4); // Extracts characters from index 1 to 3
- String Comparison: The
equals()
method is used to check if two strings have the same content.
javaboolean isEqual = str1.equals(str2);
- String Manipulation: The
String
class provides various methods for manipulating strings, such astoUpperCase()
,toLowerCase(
)
,replace()
,trim()
, etc.
javaString upperCase = str1.toUpperCase();
String replaced = str1.replace('H', 'J');
These are just some of the fundamental concepts related to strings in Java. The String
class in Java offers many more methods and functionalities for working with strings.
String All Method:
In Java, the String
class provides several useful methods for working with strings. Here is a list of some commonly used methods:
length()
: Returns the length of the string.equals()
: Check two string contain same content or not.Click here for more InformationcharAt(int index)
: Returns the character at the specified index.substring(int beginIndex)
: Returns a new string that is a substring of the original string, starting from the specified index.(Click here for more Information)Click here for more Informationsubstring(int beginIndex, int endIndex)
: Returns a new string that is a substring of the original string, starting from the beginIndex and ending at the endIndex (exclusive).Click here for more InformationindexOf(int ch)
: Returns the index of the first occurrence of the specified character, or -1 if the character is not found.Click here For More InformationindexOf(int ch, int fromIndex)
: Returns the index of the first occurrence of the specified character, starting from the specified index.Click here For More InformationindexOf(String str)
: Returns the index of the first occurrence of the specified substring, or -1 if the substring is not found.Click here For More InformationindexOf(String str, int fromIndex)
: Returns the index of the first occurrence of the specified substring, starting from the specified index.Click here For More InformationlastIndexOf(int ch)
: Returns the index of the last occurrence of the specified character, or -1 if the character is not found.lastIndexOf(int ch, int fromIndex)
: Returns the index of the last occurrence of the specified character, starting from the specified index.lastIndexOf(String str)
: Returns the index of the last occurrence of the specified substring, or -1 if the substring is not found.lastIndexOf(String str, int fromIndex)
: Returns the index of the last occurrence of the specified substring, starting from the specified index.toUpperCase()
: Converts the string to uppercase.toLowerCase()
: Converts the string to lowercase.trim()
: Removes leading and trailing whitespace from the string.startsWith(String prefix)
: Checks if the string starts with the specified prefix.endsWith(String suffix)
: Checks if the string ends with the specified suffix.contains(CharSequence sequence)
: Checks if the string contains the specified sequence of characters.replace(char oldChar, char newChar)
: Replaces all occurrences of the specified oldChar with the newChar.replaceAll(String regex, String replacement)
: Replaces all occurrences of the specified regular expression with the replacement string.
0 Comments