Published on Nov 06, 2022
E.g.:
String var = “Welcome to Java”
Java String provides various methods to perform different operations on the Strings.
Get the Length of the String
E.g. String var = “Welcome to Java”
Int length = var.length();
Join Two Java Strings
String first = “Welcome”
String second =” To Java”
String joinedString=first.concat(second);
Compare Two Strings
The String can be compared in java using the equals() method.
String first =” Java Programming”;
String second= “Java Programming”;
String third= “Python Programming”;
boolean result1=first.equals(second);
boolean result2=first.equals(third);
In Java, Strings are immutable. This means, once we create a string, we cannot change that string.
E.g. Create a string
String example=”Hello”;
example=example.concat(“World”);
Here we are using concat () method to add string world to the previous string Hello.
E.g. String name= new String(“Java Programming”);
In Java, the JVM maintains a string pool to store all strings inside the memory. The String pool helps in reusing the strings.
Here we directly provide the value of the string. Hence the compiler first checks the string pool to see if the string already exists.
If the String already exists, the new string is not created. Instead, the new reference points to the already existing sting in the memory.
If the string does not exist, a new string will be created.
Methods | Description |
---|---|
contains() | checks whether the string contains a substring |
substring() | returns the substring of the string |
join() | join the given strings using the delimiter |
replace() | replaces the specified old character with the specified new character |
replaceAll() | replaces all substrings matching the regex pattern |
replaceFirst() | replace the first matching substring |
charAt() | returns the character present in the specified location |
getBytes() | converts the string to an array of bytes |
indexOf() | returns the position of the specified character in the string |
compareTo() | compares two strings in the dictionary order |
compareToIgnoreCase() | compares two strings ignoring case differences |
trim() | removes any leading and trailing whitespaces |
format() | returns a formatted string |
split() | breaks the string into an array of strings |
toLowerCase() | converts the string to lowercase |
toUpperCase() | converts the string to uppercase |
valueOf() | returns the string representation of the specified argument |
toCharArray() | converts the string to a char array |
matches() | checks whether the string matches the given regex |
startsWith() | checks if the string begins with the given string |
endsWith() | checks if the string ends with the given string |
isEmpty() | checks whether a string is empty of not |
intern() | returns the canonical representation of the string |
contentEquals() | checks whether the string is equal to charSequence |
hashCode() | returns a hash code for the string |
subSequence() | returns a subsequence from the string |
Presentations
Browse LSET presentations to understand interesting…
Explore Now
eBooks
Get complete guides to empower yourself academically…
Explore Now
Infographics
Learn about information technology and business…
The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
Error: Contact form not found.
[wpforms id=”9030″]