Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 5 April 2017

String Methods in Java

 String class methods in java

In java, there are lots of methods in java.lang.String class. By using these string methods we can perform many operations on the string such as length, trim, concatenating, sub-string, compare, replacing string etc.

String is a most important concept in java because everything is treated as a string if you submit any query or form in web-based, window-based applications.

Let's discuss string methods.


String Methods in Java



1) Java String length

Java string length() method is used to get the total numbers of characters of any given string.

Signature Signature of string length() method is given below.

public int length()

return

this method returns the length of characters.

For example :

class StringLengthExample
{
public static void main(String args[])
{
String s1 = "India";
String s2 = "Java";
System.out.println(s1.length());//length is 5 of India string
System.ou.println(s2.length());//length is 4 of Java string
}
}

output : 5
              4


2) Java String charAt

Java string charAt() method is used to get the character at the given index number.

Signature : Signature of string charAt() method is given below.

public char charAt(int index)

Parameter : Index number, start with 0.

return 

char value

Throws 

StringIndexOutOfBoundsException : If the index is greater than string length or negative value.

For example :

class CharAtExample
{
public static void main(String args[])
{
String s1 = "javatutorial";
char c = s1.charAt(7);//returns char value at the 4th index
System.out.println(c);
}
}

output : o


3) Java String compareTo

Java string compareTo() method compare the given string with current string lexicographically. It returns positive, negative and 0 numbers.

This method compares the string on the basis of the Unicode value of each character in the string.

if String1 > String2, it returns positive number

if String1 < String2, it returns a negative number

if String1 == String2, return 0

Signature : Signature of string compareTo() method is given below.

public int compareTo(String anotherString)

return 

return an integer value.

For example :

class CompareToExample
{
public static void main(String args[])
{
String s1 = "hello";
String s2 = "hello";
String s3 = "meklo";
String s4 = "hemlo";
String s5 = "flag";

System.out.println(s1.compareTo(s2));//0 because both are equal
System.out.println(s1.compareTo(s3));//-5, h is 5 times lower than m
System.out.println(s1.compareTo(s4));//-1, l is 1 times lower than m
System.out.println(s1.compareTo(s5));//2, h is 2 times greater than f
}
}

output : 0
             -5
             -1
             -2


4) Java String concat

Java string concat() method is used to combines two string. In other words, we can say, this method combines specified string at the end of this string.

Signature : Signature of string concat() method is given below.

public String concat(String anotherString)

return

returns combined string

For example :

class ConcatExample
{
public static void main(String args[])
{
String s1 = "Siya";
String s2 = "Singh";
System.out.println(s1.concat(s2));
}
}

output : SiyaSingh

5) Java String contains

Java string contains() method searches the sequences of characters in the string. It returns true if the sequence is found otherwise returns false.

Signature : Signature of string contains() method is given below.

public boolean contains(CharSequence sequence) 

return

return true, if the sequence is found otherwise false.

For example :

class ContainsExample
{
public static void main(String args[])
{
String s = "Java is a high level programming language";
System.out.println(s.contains("high level"));
System.out.println(s.contains("programming"));
System.out.println(s.contains("low level"));
}
}

output : true
               true
               false


6) Java String endsWith

Java string endsWith() method return true if a string is end with given another string or suffix, otherwise, it returns false.

Signature : Signature of string endsWith() is given below.

public boolean endsWith(String suffix)

return

true of false

For example : 

class EndsWithExample
{
public static void main(String args[])
{
String s = "India is good country";
System.out.println(s.endsWith("y"));
System.out.println(s.endsWith("try"));
System.out.println(s.endsWith("country"));
System.out.println(s.endsWith("z"));
}
}

output : true
              true
              true
              false


7) Java String equals

Java string equals() method is used to compares the two given string based on the content of the string. It returns true if all the characters are matched, otherwise return false. This method is case sensitive.

Signature : Signature of string equals() method is given below.

public boolean equals(Object anotherObject)

return

true, if both strings are matched, otherwise give false.

For example :

class EqualsExample
{
public static void main(String args[])
{
String s1 = "hello";
String s2 ="hello";
String s3= "HELLO";
String s4 ="go";
System.out.println(s1.equals(s2));//true,both are same
System.out.println(s2.equals(s3));//false, case in not same
System.out.println(s3.equals(s4));//false, content is not matched 
}
}

output : true
              false
              false


8) Java String equalsIgnoreCase

Java string equalsIgnoreCase() method is just like equals method but this method does not check case. It returns true if all characters are matched otherwise returns false.

Signature : Signature of string equalsIgnoreCase() method is given below.

public boolean equalsIgnoreCase(String str)

return

If all characters are matched, return true otherwise false.

For example :

class EqualsIgnoreCaseExample
{
public static void main(String args[])
{
String s1 = "hello";
String s2 ="hello";
String s3= "HELLO";
String s4 ="go";
System.out.println(s1.equalsIgnoreCase(s2));//true,content are same
System.out.println(s2.equalsIgnoreCase(s3));//true, case ignored
System.out.println(s3.equalsIgnoreCase(s4));//false, content not sam
}
}

output : true
              true
              false


9) Java String toLowerCase

Java string toLowerCase() method is used to convert upper case string into lower case string.

Signature : Signature of string toLowerCase() method is given below.

public String toLowerCase()

return

return string in lower case.

For example :

class ToLowerCaseExample
{
public static void main(String args[])
{
String s1 = "HELLO";
System.out.println(s1.toLowerCase());
}
}

output : hello


10) Java String toUpperCase


Java string toUpperCase() method is used to convert lower case string into upper case string.

Signature Signature of string toUpperCase() method is given below.

public String toUpperCase()

return

return string in upper case letter.

For example :

class ToUpperCaseExample
{
public static void main(String args[])
{
String s1 = "hellojava";
System.out.println(s1.toUpperCase());
}
}

output : HELLOJAVA


11) Java String getBytes

Java String getBytes() method returns the byte array of any string in java i.e it returns a sequence of bytes.

Signature : Signature of string getBytes() method is given below.

public byte[] getBytes()

public byte[] getBytes(Charset charset)

return

return sequence of bytes.

For example :

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


12) Java String substring

Java string substring() method returns the part of the string. 

We pass start index and end index number position in the substring method, where start index is inclusive and end index is exclusive.

There are 2 types of substring method.

Signature : Signature of string substring() method is given below.

public String substring(int startIndex)

public String substring(int startIndex, int endIndex)

If you don't specify endIndex in substring method, substring method will return all the characters from startIndex.

return

return specifies string.

For example :

class SubstringExample
{
public static void main(String args[])
{
String s = "programming";
System.out.println(s.substring(3));//start index
System.out.println(s.substring(3,9));//start and end index
}
}

output : gramming
              grammi


13) Java String startsWith

Java string startsWith() method return true if a string is start with given another string or prefix, otherwise this method returns false.

Signature : Signature of string startsWith() method is given below.

public boolean startsWith(String prefix)

public boolean startsWith(String prefix, int offset)

return

return true or false

For example :

class StartsWithExample
{
public static void main(String args[])
{
String s = "India is good country";
System.out.println(s.startsWith("In"));
System.out.println(s.startsWith("India is good"));
System.out.println(s.startsWith("good"));
System.out.println(s.startsWith("dia"));
}
}

output : true
              true
              false
              false


14) Java String replace

Java string replace() method returns a string replacing all the old char or CharSequence to a new char or CharSequence.

Signature : Signature of string replace() method is given below.

public String replace(char oldChar, char newChar)

public String replace(CharSequence target, CharSequence replacement)

return

returns replaced string .

For example 1 :

String replace(char old, char new) method example.

class ReplaceExample1
{
public static void main(String args[])
{
String s = " java is programming language";
System.out.println(s.replace('a','o'));//replace 'a' to 'o'
}
}

output : jovo is progromming longuoge

For example 2 :

String replace(CharSequence target, CharSequence replacement) method example.

class ReplaceExample2
{
public static void main(String args[])
{
String s = " java was programming language";
System.out.println(s.replace("was" "is"));//replace "was" to "is"
}
}


output : java is programming language


15) Java String trim

Java string trim() method remove the leading(starting spaces) and trailing(ending spaces) spaces in the string.

Signature : Signature of string trim() method is given below.

public String trim()

return

string without leading and trailing spaces.

For example :

class TrimExample
{
public static void main(String args[])
{
String s = "   hello java   ";
System.out.println(s+"javaprogmming");

System.out.println(s.trim()+"javaprogmming");
}

output : hello java    javaporgramming
            hello javajavaporgramming

16) Java String indexOf

Java string indexOf() method return index of given character value or substring. It returns - 1, it is not found.

Signature : Signature of string indexOf() is given below.

int indexOf(int ch)

This method returns index position for the given char value.

int indexOf(int ch, int fromIndex)

This method returns index position for the given char value and from the index.

int indexOf(String substring)

This method returns index position for the given substring.

int indexOf(String substringint fromIndex)

This method returns index position for the given substring and from the index.

return 

Index of the string.

For example :

class IndexOfExample
{
public static void main(String args[])
{
String s = "this is index of example";

//passing substring
int index1 = s.indexOf("is");//return the index of is substring
int index2 = s.indexOf("index");//return index of index substring
System.out.println(index1+" "+index2);//2 8

//passing substring with from index
int index3 = s.indexOf("is",4);//return index of is substring after 4th index
System.out.println(index3);//5 i.e the index of another is

//passing char value
int index4 = s.indexOf('s');//return index of s char value
System.out.println(index4);
}
}



output : 2 8

               5

               3

17) Java String lastIndexOf

Java string lastIndexOf() method return last index of given character value or substring. It returns - 1, it is not found.



Signature Signature of string lastIndexOf() is given below.

int lastIndexOf(int ch)

This method returns last index position for the given char value.


int lastIndexOf(int ch, int fromIndex)

This method returns last index position for the given char value and from the index.


int lastIndexOf(String substring)

This method returns last index position for the given substring.

int lastIndexOf(String substring, int fromIndex)

This method returns last index position for the given substring and from the index.

return 

returns last index of the string

For example :

class LastIndexOfExample
{
public static void main(String args[])
{
String s = "this is index of example";//there is 2 's' in sentence

//return last index of 's' char value
int index1 = s.lastIndexOf('s');

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate