Translate

Tuesday, June 7, 2016

Some basic methods of String class.

Method-1 toCharArray()

toCharArray() is a method of string class which is used for convert a string into a Character type array.It returns a character type array.

example-  String s="hello";

char c=s.toCharArray();

o/p {'h','e','l','l','o'};

Method-2 charAt(int i)

charAt() method is used for get the character at the specific index.It returns a character.

example-  String s="hello";

char c=s.charAt(0);

o/p-  h

Method-3 length()

length() method is used for find the total length of string.It returns a integer number

example-  String s="hello";

int length=s.length();

o/p- 5

Method-4 toUpperCase() & toLowerCase()

Both methods are used for changing the case of a string.Both methods return String.

String s="hello";

String s1="HELLO";

s=s.toUpperCase();  // o/p  HELLO

s1=s1.toLowerCase();  // o/p  hello


Method-5 replace(String old, String new)

This method is used for replace a existing string with a new String.It returns a string.

String s="hello";

s=s.replace("hello","bye");

o/p bye

Method-5 split(regular expression)


This method is used for split the string according to regular expression and it returns a string type array.

String s="hello, how are you";

String splitarray[]=s.split("[\\S][ ]+");

or

String splitarray[]=s.split(" ");

o/p {"hello,","how","are","you"};



No comments:

Post a Comment