Java String is the class you need to access and manipulate strings. A string is a sequence of characters. In Java strings are object and not array as some other programming languages.  Every time you create a string, that cannot be changed. Insted, every time you wand to change it another String object will be created with the modified version of the string. 

String Constructors

There are many way you can create a String an so there are many constructors. These are vrious way you can create instances of String

String str = new String(); //create an empty string

char ch[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
String str = new String(ch); //str is initialized to abcdefg

char ch[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
String str = new String(ch, 3, 5); //str is initialized to def

char ch[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
String str1 = new String(ch); 
String str2 = new String(str1); // str2 is initialized to abcdefg

String str = "abcdefg"; //str is initialized to abcdefg

String Concatenation

In Java you can concatenate your strings with the + operator. You can also concatenate different data Types like this:

String str = "abcde" + 123.45 + " fghi"; // str is abcde1234.45 fghi



Many time you will need to know the lenght of a given string. You can simply use the length() method. This will return an int with the number of characters of the string.

char ch[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
String str = new String(ch); 
int len = str.length();// len will bi 7

Java create an Object String  also with literal so you can use this method direcly  with string literal.

int len = "abcdefg".length();


With this method that you can override in your classes, you can return a personalized string for your an object

class Cl {
   int x;
   int y;

   Cl(int t, int z) {
      x = t;
      y = z;

   public String toString() {
      return "The coordinates are: x=" + x +
      ", y=" + y;


class demo {
   public static void main(String args[]){
      Cl c = new Cl(10, 20);


and the output will be

The coordinates are: x=10, y=20


void getChars(int startSrc, int endSrc, char tr[], int startTrg)

This method extract multiple characters from a string

  • startSrc is the index of the string you want to start extract.
  • endSrc is the index of the string you want to end extract
  • tr[] is the array that will hold the characters
  • startTrg is the index of tr[] where to start putting the characters
Make sure th[] is big enough to contain the string


This method extract a single character like this

char c;
c = "abcdefghi".charAt(4); // the value of c is e


This will transform all the characters in a string into a char Array

char[] c = "abcdefg".toCharArray;


static String join(CharSequence delimiter, CharSequence ... s)

here delimiter separate the strings and s are the characters sequences to join

toUpperCase() and toLowerCase()

As may you imagine, these two methods create a new string with all Upper or Lower case

String str = "abcdefg";
System.out.println(str.toUpperCase); // ABCDEFG

String str2 = "ABCDEFG";
System.out.println(str.toLowerCase); // abcdefg


Use thise method to convert data in different forms into a string.

Check also Java Control Statements


Concatenate two strings. It's basically like using the + operator.

String contact(String s)


With this you can replace the occurrences of one or multiple characters

String replace(char source, char repl);

String replace(CharSequence source, CharSequence replace);


You can specify the start point at which extract the string or both start and end

String substring(int indx);

String substring(int start, int end);


Trim method remove all trailing and leading whitespace from the given string

String trim()

indexOf() and lastIndexOf()

These two methods search for the first and the last occurrence of a character or a substring

int indexOf(String s);
int lastIndexOf(String s);


compareTo compare two string and return an int as result as this

int i = str.compareTo(string);
  • i < 0 // str is less than string
  • i > 0  // str is greater than string
  • i = 0 // str is equal to string

startWith() and endsWith()

With this two methods you can check if a String start or end with a specified string

equals() equalsIgnoreCase()

they return true or false on if a string is equals or not to the other. equalsIgnoreCase() will ignore the case.


This compare specific part inside a string with specific part of the other

boolean regionMatch(int start, String string2,
                                   int startstr2, int nChar);

Java Control Statements



Categories: Java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: