String And Regex In Java

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.

  1. String 
  2. StringBuffer
  3. 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

  1. By string Literal
  2. 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.MethodsDescription
1Char charAt(int index)Returns Character at the specified  index in  the string
2int length()Return the total number of characters in the string
3String format(String format, object ..arg)Return formatted the string with providing a locale
4String substring(int begin, int end)Return sequence of characters from specified beginning index to end index in form of the string object
5Boolean contains(CharSequences)Return true if specified char sequences are present in string else return false
6String substring(int begin)Return sequence of characters from the specified beginning to end of the string in form of the string object
7String  join(CharSequence)Return string object by joining specified string to old string
8String equals()Check  string are equal or not
9String isEmpty()Return true of the characters of the element in the string is 0 elseReturn false
10String Concat(String str)Return string after joining the string 
11String replace(char old, char new)Return new string by changing old character by new character 
12String equalsIgnoreCase(String str)Return true if the string is the same after ignoring Case ex java, Java, JAVA, JaVa are considered the same
13String []split(String regex)Return array of the string by splitting by specified regex
14int indexOf(int char,int begin)Return index of specified char after the specified beginning
15String toLowerCase(String str)Return string with all lowercase element
16String toUpperCase(String str)Return string with all upper case element
17String trim()Return string after removing spaces from the beginning and end of the string
18Static 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: –

  1. append() –  This is used concatenates the string.
  2. insert()-      This is used to insert a given string in a specified string.
  3. delete()-     Deletes the specified string specified begin and end.
  4. 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

  1. Matcher class
  2. Pattern class
  3. MatchResult interface

Matcher Class

No.Method Description
1boolean matches()Check whether the pattern matches the regular expression
2find()Return true if the pattern matches the next expression
3boolean find(int start)Return true if the pattern matches the next expression from the given start position
4String group()Return matched string
5int startReturns starting position of matched expression
6int end()Returns ended position of matched expression
7groupCount()Return total matched substring in a given string

Pattern Class

Pattern class is a regular expression compiled expression.

It has the following methods:-

  1. Static Pattern compile(string regex)- it is used to compile the given regex for pattern matching
  2. Matcher matcher(string input) – It used to match given input with the regular expression pattern
  3. 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
  4. String pattern() – return the regular expression
  5. 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

Leave a Comment

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