String is generally sequence of a character, in java it is a class. unlike c or c++ at the end of the string the ‘\0’ character is not placed in java.
String is simply defined like
String str = “myString”;
To find length of a string use length() function.
so str.length() will give me 8 as a string length. like array string also has indexes which starts with 0,
to find character at specific index use charAt(i) function where i is the index of the character.
now lets see the functionality of these 2 String fictions with the help of an example.
public class myString { public static void main(String[] args) { int n=str.length(); String str1="this is a java"; // find character at 3 & length String str2="hi i am here";// find character at 2 & length String str3="where are you";// find character at 0 & length String str4="this is a demo";// find character at 5 & length String str5="core java";// find character at 6 & length System.out.println("Character at 3 of str1 = "+str1.charAt(3) +" | Length is :" + str1.length()); System.out.println("Character at 2 of str2 = "+str2.charAt(2) +" | Length is :" + str2.length()); System.out.println("Character at 0 of str3 = "+str3.charAt(0) +" | Length is :" + str3.length()); System.out.println("Character at 5 of str4 = "+str4.charAt(5) +" | Length is :" + str4.length()); System.out.println("Character at 6 of str5 = "+str5.charAt(6) +" | Length is :" + str5.length()); } }
output:
Character at 3 of str1 = s | Length is :14
Character at 2 of str2 = | Length is :12
Character at 0 of str3 = w | Length is :13
Character at 5 of str4 = i | Length is :14
Character at 6 of str5 = a | Length is :9
We can convert string to upper case and lower case too using toUpperCase() and toLowerCase() functions. Consider below example.
public class JavaPrograms { public static void main(String[] args) { String str1="this is a java"; String str2="HI I AM VIPULA"; System.out.println("Str1 is:'"+str1+"'"); System.out.println("Str1 in lower case:'"+str1.toUpperCase()+"'"); System.out.println("Str2 is:'"+str2+"'"); System.out.println("Str2 in lower case:'"+str2.toLowerCase()+"'"); } }
output :
Str1 is:’this is a java’
Str1 in lower case:’THIS IS A JAVA’
Str2 is:’HI I AM VIPULA’
Str2 in lower case:’hi i am vipula’
Another more important function of string is subsring, it has 2 forms
- substring(int beginIndex) : it will return a string starting with beginIndex till the end of the string
- substring(int beginIndex,int endIndex) : it will return a string starting with beginIndex till the endIndex (practically if you seeyou will observe that in this case the substring starts with the startIndex and it takes till next (endIndex-startIndex) characters. e.g. if i am taking str.substring(3,9) the string will start from 3rd index and will take till (9-3=6) i.e. till next 6 characters.)
Let us understand this with example
public class mySubString { public static void main(String[] args) { String str="this is ajava"; System.out.println(str.substring(3)); System.out.println(str.substring(3,9)); } }
output :
s is ajava
s is a
Lets see one more example. with the help of sub string print below pattern (Make your program generalized in such a way that it should work for any string)
amol
mola
olam
lamo
public class mySubString { public static void main(String[] args) { String str="amol"; for(int i=0;i<str.length();i++) System.out.println(str.substring(i)+str.substring(0,i)); } }
output:
amol
mola
olam
lamo
Another important function of String is split. its syntax is str.split(“ch “). it will return an array of a string spitted by the character you specify inside spit. lets understand this with the help of example.
public class SplitFunction { public static void main(String[] args) { String str="this is a java"; String[] strArr = str.split(" "); // split by space. System.out.println("Given String is '"+str+"'"); System.out.println("There are total "+strArr.length+ " words separeted by space"); for (int i=0;i<strArr.length;i++) { System.out.println(strArr[i]); } System.out.println("_____________________________"); String str1="12-2-2016"; System.out.println("Given String is '"+str1+"'"); String[] strArr1 = str1.split("-"); // split by space. System.out.println("There are total "+strArr1.length+ " words separeted by -"); for (int i=0;i<strArr1.length;i++) { System.out.println(strArr1[i]); } } }
output:
Given String is ‘this is a java’
There are total 4 words separeted by space
this
is
a
java
_____________________________
Given String is ’12-2-2016′
There are total 3 words separeted by –
12
2
2016
Lets see one more example on this. consider a string “Hey baby you are so sweet” find out the words in this string ending with ‘y’.(dont use in built function.)
public class SplitExample { public static void main(String[] args) { String str="Hey baby you are so sweet"; String[] strArr=str.split(" "); for (int i=0; i<strArr.length;i++) { if(strArr[i].charAt(strArr[i].length()-1)=='y') System.out.println(strArr[i]); } } }
output:
Hey
baby
Capgemini interview Question
How will you reverse a string without using in built functions.
public class StringReverse { public static void main(String[] args) { String str="MyString"; String reverse=""; int length = str.length(); for ( int i = length - 1 ; i >= 0 ; i-- ) reverse = reverse + str.charAt(i); System.out.println("Given string is: "+str); System.out.println("Reverse of entered string is: "+reverse); } }
output:
Given string is: MyString
Reverse of entered string is: gnirtSyM