X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Concatenation methods :-

Concatenation refers to combining Strings.

It can be done by two ways :

  • concat()




  • + operator




concat() :

This method is used to concatenate one String with another String.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = "Algo"; String s2 = s1.concat("Java"); System.out.println(s1); System.out.println(s2); } }

Output :

Algo AlgoJava

+ operator :

This operator is also used to concatenate one String with another String.

This operator is the only overloaded operator in Java (sometimes it acts as String concatenation operator and sometimes it acts as arithmetic addition operator).

If atleast one argument is String type then + operator acts as String concatenation operator.

If both arguments are number type then + operator acts as arithmetic addition operator.

The operation of arguments are done from left to right.

Example 1 :

class StringExample { public static void main(String[ ] args) { String a = "Algo"; int b = 1; int c = 2; int d = 3; System.out.println(a+b+c+d); System.out.println(b+c+d+a); System.out.println(c+d+a+b); System.out.println(d+a+b+c); } }

Output :

Algo123 6Algo 5Algo1 3Algo12

Example 2 :

class StringExample { public static void main(String[ ] args) { String a = "Algo"; int b = 1; int c = 2; int d = 3; a = a+b+c+d; b = b+c+d; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }

Output :

Algo123 6 2 3 Note :

If we try to assign 'b+c+d' to 'a' then we will get compile time error saying : CE: incompatible type required : java.lang.String found : int

If we try to assign 'a+b+c+d' to 'b' then we will get compile time error saying : CE: incompatible type required : int found : java.lang.String


Extracting character or String from a String :


charAt() :-

This method is use to extract character from a String.

It returns the character value at specified index.

Example :

import java.util.*; class StringExample { public static void main(String[ ] args) { int count = 0; Scanner s = new Scanner(System.in); System.out.print("Enter a String : "); String str = s.next(); for(int i = 0 ; i < str.length() ; i++) { switch(str.charAt(i)) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : count++; } } System.out.println("Number of vowels in the String : "+count); } }

Output :

Enter a String : Algo Number of vowels in the String : 1

substring(int beginIndex) :-

It returns a new String that is a substring of the given String by specifying starting point(beginIndex) which will return all characters from starting point(beginIndex) to the end of the String.


substring(int beginIndex , int endIndex) :-

It returns a new String that is a substring of the given String by specifying starting point(beginIndex) and ending point(endIndex) which will return all characters from starting point(beginIndex) to one less than the ending point(endIndex).

Example :

class StringExample { public static void main(String[ ] args) { String s = "Algovalley"; System.out.println(s.substring(4)); System.out.println(s.substring(2,10)); System.out.println(s.substring(0)); } }

Output :

valley govalley Algovalley

trim() :

It removes the blank space present at the beginning and end of the String but don't remove blank space at the middle of the String.

Example :

class StringExample { public static void main(String[ ] args) { String s = " Algo Valley "; System.out.println(s.trim()); } }

Output :

Algo Valley

indexOf(char ch) :

It returns index of first occurrence of the specified character (char ch) .

Example :

class StringExample { public static void main(String[ ] args) { String s = "algovalley"; System.out.println(s.indexOf('a')); } }

Output :

0

lastIndexOf(char ch) :

It returns index of last occurrence of the specified character (char ch) .

Example :

class StringExample { public static void main(String[ ] args) { String s = "algovalley"; System.out.println(s.lastIndexOf('a')); } }

Output :

5

Conversion methods :


toLowerCase() :-

It is a method which convert all the characters in the String to lower case using the rules of default locale (locale is an object in Java which define language and default locale is "English").


toUpperCase() :-

It is a method which convert all the characters in the String to upper case using the rules of default locale (locale is an object in Java which define language and default locale is "English").

Example :

class StringExample { public static void main(String[ ] args) { String s = "Algo"; System.out.println(s.toLowerCase()); System.out.println(s.toUpperCase()); } }

Output :

algo ALGO

Replacing methods :


replace(char oldChar , char newChar) :-

It is a method which return new String by replacing all the occurrences of old character(oldChar) in the String with new character(newChar).


replaceFirst(String regex , String replace) :-

It is a method which return new String by replacing the first substring which matches the given regular expression(regex) with the given replacement(replace).

Example :

class StringExample { public static void main(String[ ] args) { String s = "AlgoValley"; System.out.println(s.replace('V' , 'v')); System.out.println(s.replaceFirst("Valley" , "Java")); } }

Output :

Algovalley AlgoJava

String[ ] split(String regex) :-

It is a method which splits the String around matches of the given regular expression(regex).

Example 1 :

class StringExample { public static void main(String[ ] args) { String s1 = "AlgoValley"; String s2[ ] = s1.split("V"); System.out.println(s2.length); for(int i = 0 ; i < s2.length ; i++) System.out.println(s2[i]); } }

Output :

2 Algo alley

Example 2 :

class StringExample { public static void main(String[ ] args) { String s1 = "A L G O V A L L E Y"; String s2[ ] = s1.split(" "); System.out.println(s2.length); for(int i = 0 ; i < s2.length ; i++) System.out.println(s2[i]); } }

Output :

10 A L G O V A L L E Y

byte[ ] getBytes() :-

It is a method which encodes the String into a sequence of bytes using platform's default charset, storing the result into a new byte array.

X

encodes :

It refers to converting (information or an instruction) into a particular form.

X

charset (Character Set) :

A character set is a set of symbols and encodings.

Example :

class StringExample { public static void main(String[ ] args) { String s = "ALGO"; byte b[ ] = s.getBytes(); System.out.println(s); for(int i = 0 ; i < b.length ; i++) System.out.println(b[i]); } }

Output :

ALGO 65 76 71 79


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.