Java string
A Java String Is Considered As An Object But It Represents An Array Of Characters.
There are 3 ways to represent the sequence of characters.
- String
- StringBuffer
- StringBuilder
Java String is immutable ( not changed). Whenever we make changes in string in java a new instance is created. Due to this reason when we need too many changes required to make in the string we use StringBuffer and StringBuilder as they are mutable.
Creation of String Object
- By string Literal
- By new Keyword
String Literal
String literal used double-quotes for creation.
Whenever a string literal is created, the JVM checks for the string constant pool. If a string already exists in the pool, a reference for it exists in the pool is returned. It does not exist a new string instance is created.
By new keyword
By using a new keyword JVM will always create a new string object in heap memory without checking for the pool.
Java String class Methods
No. | Methods | Description |
1 | Char charAt(int index) | Returns Character at the specified index in the string |
2 | int length() | Return the total number of characters in the string |
3 | String format(String format, object ..arg) | Return formatted the string with providing a locale |
4 | String substring(int begin, int end) | Return sequence of characters from specified beginning index to end index in form of the string object |
5 | Boolean contains(CharSequences) | Return true if specified char sequences are present in string else return false |
6 | String substring(int begin) | Return sequence of characters from the specified beginning to end of the string in form of the string object |
7 | String join(CharSequence) | Return string object by joining specified string to old string |
8 | String equals() | Check string are equal or not |
9 | String isEmpty() | Return true of the characters of the element in the string is 0 elseReturn false |
10 | String Concat(String str) | Return string after joining the string |
11 | String replace(char old, char new) | Return new string by changing old character by new character |
12 | String equalsIgnoreCase(String str) | Return true if the string is the same after ignoring Case ex java, Java, JAVA, JaVa are considered the same |
13 | String []split(String regex) | Return array of the string by splitting by specified regex |
14 | int indexOf(int char,int begin) | Return index of specified char after the specified beginning |
15 | String toLowerCase(String str) | Return string with all lowercase element |
16 | String toUpperCase(String str) | Return string with all upper case element |
17 | String trim() | Return string after removing spaces from the beginning and end of the string |
18 | Static String valueOf(int value) | Return the type of string |
StringBuffer Class
StringBuffer is a mutable string. It is used when we have to make too many changes in the string. It is also thread-safe. This means multiple threads cannot access is simultaneous.
It has the following methods: –
- append() – This is used concatenates the string.
- insert()- This is used to insert a given string in a specified string.
- delete()- Deletes the specified string specified begin and end.
- reverse()- Reverse the string.
StringBuilder Class
StringBuilder class is used for creating the modifiable string (mutable). It is made available after the JDK 1.5. however, It is the same as StringBuffer but the only difference is that It is thread-safe.
It has the following methods:-
- append() – This is used concatenates the string.
- insert()- This is used to insert a given string in a specified string.
- delete()- Deletes the specified string specified begin and end
- reverse()- Reverse the string.
Regex In Java
Regex’s full form is Regular Expression. This is an API (Application program interface) for the pattern searching or manipulation of strings.
It is in java.util.regex main use for validation of email, password, etc
Java.util.regex package contains
- Matcher class
- Pattern class
- MatchResult interface
Matcher Class
No. | Method | Description |
1 | boolean matches() | Check whether the pattern matches the regular expression |
2 | find() | Return true if the pattern matches the next expression |
3 | boolean find(int start) | Return true if the pattern matches the next expression from the given start position |
4 | String group() | Return matched string |
5 | int start | Returns starting position of matched expression |
6 | int end() | Returns ended position of matched expression |
7 | groupCount() | Return total matched substring in a given string |
Pattern Class
Pattern class is a regular expression compiled expression.
It has the following methods:-
- Static Pattern compile(string regex)- it is used to compile the given regex for pattern matching
- Matcher matcher(string input) – It used to match given input with the regular expression pattern
- static boolean matches(String regex, String input) – It combines the compile and matcher method. It used to match given input with the regular expression pattern
- String pattern() – return the regular expression
- String split(string input)- return an array of strings with split substring from the string around the matches of a given pattern.
Conclusion | String And Regex In Java
thus, In java, the string is immutable so java comes up with a mutable class like StringBuffer and StringBuilder. however, The only difference between StringBuffer and StringBuilder is that StringBuffer is not thread-safe but StringBuilder is thread-safe.
hence, There are so many methods pre implemented for String And Regex In Java in convenience like length(), format(), equals(), append(), remove(), Concat(), etc. Regex is an API for the manipulation and searching of patterns.
Written By: Charchil Gupta
Reviewed By: Soutik Maity
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs