Learn strings in java

Hello everyone. In this post we are going to learn strings in java.

String is a sequence of characters which represents character values. To create string in java, java.lang.String package is used.

Objects of String are immutable in java. There are two ways to create string,

  • String literal
  • using “new” keyword

Also read – learn string literal in java

// string literal
String name = "George";

Learn Strings In Java

Another is using “new” keyword,

String name = new String("Hello World");

Here’s an example on declaring a string,

public class StringDemo 
{
   public static void main(String[] args) 
   {
      // declaring String without "new" keyword 
      String name = "George";
      // printing string name 
      System.out.println("String name = " + name);
      // declaring String using "new" keyword 
      String strName = new String("FlowerBrackets");
      // printing string strName
      System.out.println("String strName = " + strName);
   }
}

Output:

String name = George
String strName = FlowerBrackets


String class implements CharSequence, Serializable and Comparable interface.

StringBuffer class, StringBuilder class and String class are used to create string in java. These three classes implement CharSequence interface.


String class methods

MethodDescription
static String valueOf(int value)this overloaded method converts given type to string
char charAt(int index)this method returns char value at specified index.
String trim()this method removes leading and trailing whitespace.
int length()returns length of given string.
String toUpperCase(Locale locale)converts all characters in the given string to upper case using rules of given Locale.
static String format(String format, Object... args)returns formatted string using specified format string and arguments.
String toUpperCase()converts all the characters to upper case.
static String format(Locale l, String format, Object... args)returns formatted string using the specified locale, format string, and arguments.
String toLowerCase(Locale l)converts all the characters to lower case using rules of given Locale.
String substring(int beginIndex)returns a new string that is substring. substring begins with given beginIndex.
String substring(int beginIndex, int endIndex)returns new string that is substring with given beginIndex and endIndex.
boolean isEmpty()checks if length() is 0, otherwise false.
String concat(String str)concatenates specified string to end of given string.
boolean equals(Object anObject)Compares this string to the specified object.
String replace(CharSequence target,CharSequence replacement)replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String intern()returns a canonical representation for the string object.
int indexOf(int ch)returns index of the first occurrence of the specified character.
String toLowerCase()converts all the characters of String to lower case.
String replace(char oldChar, char newChar)replaces all occurrences of oldChar in given string with newChar.

Also read – string constructors in java

Reference – Oracle Docs

Related Posts