Learn strings in java

Welcome to flower brackets blog. Today we are going to learn strings in java.

String is an object. It’s a sequence of characters which represents character values. To create string in java, java.lang.String package is used. There are two ways to create string, one is String literal,

Also read – learn string literal in java

// String literal example
String name = "George";

Another is using “new” keyword,

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


Now there are some properties and functions related to strings which we can use.

String methods

int java.lang.String.length() : Returns the length of the string.

public class StringLength
{
   public static void main(String[] args)
   {
      String strExample = "Flower Brackets";
      int getLength = strExample.length();
      System.out.println(getLength);
   }
}

Output:

14


char java.lang.String.charAt(int index) : Returns character value at specified index. An index ranges from 0 to length – 1.
public class CharAtDemo
{
   public static void main(String[] args) 
   {
      String name = "HelloWorld";
      char ch = name.charAt(5);
      System.out.println(ch);
   }
}

Output:
W


String java.lang.String.substring(int beginIndex) : Returns a substring. Substring begins with character at specified index and extends to the end of string.
public class SubstringDemo 
{
   public static void main(String[] args) 
   {
      String str = new String("Hello world java");
      System.out.println("Substring starting from index 4 : ");
      System.out.println(str.substring(4));
   }
}

Output:
Substring starting from index 4 : o world java


String java.lang.String.substring(int beginIndex, int endIndex) : Returns a substring. Substring begins at specified beginIndex and extends to character at index endIndex – 1.

public class SubstringDemo 
{
   public static void main(String[] args) 
   {
      String str = new String("Hello world java");
      System.out.println("Substring starting from index 4 to 10 : ");
      System.out.println(str.substring(4, 10));
   }
}

Output:

Substring starting from index 4 to 10 : o worl


String java.lang.String.concat(String arg0) : Concatenates specified string to the end of the given string.

public class ConcatDemo 
{
   public static void main(String[] args) 
   {
      String str = "Hello World"; 
      str = str.concat("!! java."); 
      System.out.println(str); 
   }
}

Output:

Hello World!! java.


int java.lang.String.indexOf(String str) : Returns index within given string of the first occurrence of the specified substring.

public class IndexOfDemo 
{
   public static void main(String[] args) 
   {
      String str = new String("Dhoni is the best wicket keeper.");
      System.out.println("Index : " + str.indexOf("best"));
   }
}

Output:

Index : 13


int java.lang.String.indexOf(int ch, int fromIndex) : Returns index within given string of the first occurrence of the specified character, starting the search at the specified index.

public class IndexOfDemo 
{
   public static void main(String[] args) 
   {
      String str = new String("Dhoni is the best wicket keeper.");
      System.out.println("Index after 12th character : " + str.indexOf('e', 12));
   }
}

Output:

Index after 12th character : 14


int java.lang.String.lastIndexOf(int ch) : Returns index within given string of the last occurrence of the specified character.

public class LastIndexOfDemo 
{
   public static void main(String[] args) 
   {
      String str = new String("Dhoni is the best wicket keeper.");
      int num = str.lastIndexOf('e'); 
      System.out.println(num);
   }
}

Output:

29


boolean java.lang.Boolean.equals(Object arg0) : Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as given object.

public class BooleanEqualsDemo 
{
   public static void main(String[] args) 
   {
      Boolean a1 = new Boolean(true);
      Boolean b2 = new Boolean(false);
      if(a1.equals(b2))
      {
         System.out.println("true"); 
      }
      else
      { 
         System.out.println("false"); 
      }
   }
}

Output:

false


boolean java.lang.String.equalsIgnoreCase(String anotherString) : Compares given String to another String, ignoring case considerations.

public class EqualsIgnoreCaseDemo 
{
   public static void main(String[] args) 
   {
      String s1 = "mahendra singh dhoni";
      String s2 = "virat kohli";
      String s3 = "VIRAT KOHLI";

      boolean bool1 = s2.equalsIgnoreCase(s1);
      boolean bool2 = s2.equalsIgnoreCase(s3);

      System.out.println("s2 = s1 : " + bool1);
      System.out.println("s2 = s3 : " + bool2);
   }
}

Output:

s2 = s1 : false
s2 = s3 : true


int java.lang.String.compareTo(String anotherString) : Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

If output < 0, then "string1" comes before "string2".
output = 0, then "string1" and "string2" are equal.
output > 0, then "string1" comes after "string2".

public class CompareToDemo 
{
   public static void main(String[] args) 
   {
      String s1 = "mahendra singh dhoni";
      String s2 = "virat kohli";
      String s3 = "mahendra singh dhoni";

      int v1 = s1.compareTo(s2);
      System.out.println(v1);

      int v2 = s1.compareTo(s3);
      System.out.println(v2);

      int v3 = s2.compareTo("virat kohli");
      System.out.println(v3);
   }
}

Output:

-9
0
0


int java.lang.String.compareToIgnoreCase(String str) : Compares two strings lexicographically, ignoring case differences.

Also read – trim method

If output < 0, then "string1" comes before "string2".
output = 0, then "string1" and "string2" are equal.
output > 0, then "string1" comes after "string2".

public class CompareToIgnoreCaseDemo 
{
   public static void main(String[] args) 
   {
      String s1 = "Mahendra singh dhoni";
      String s2 = "Mahendra singh dhoni";
      String s3 = "Virat kohli";

      int output = s1.compareToIgnoreCase(s2);
      System.out.println(output);

      output = s2.compareToIgnoreCase(s3);
      System.out.println(output);

      output = s3.compareToIgnoreCase(s1);
      System.out.println(output);
   }
}

Output:

0
-9
9


String toLowerCase() and String toUpperCase() : Converts all the characters in the given string to lowercase and uppercase.

public class StringLength
{
   public static void main(String[] args)
   {
      String strExample = "Flower Brackets";
      String str1 = strExample.toLowerCase();
      String str2 = strExample.toUpperCase();
      System.out.println("Lowercase : " + str1);
      System.out.println("Uppercase : " + str2);
   }
}

Output:

Learn Strings In Java


String java.lang.String.replace(char oldChar, char newChar) : Returns string resulting from replacing all occurrences of oldChar in this string with newChar.

public class StringReplaceDemo 
{
   public static void main(String[] args) 
   {
      String str = "Mahendra,singh,dhoni,is,the,best,wicket,keeper.";
      str = str.replace(',', ' ');
      System.out.println(str);
   }
}

Output:

Mahendra singh dhoni is the best wicket keeper.


Also read – string constructors in java

Reference – Oracle Docs

Related Posts