A few string class methods

posted Jan 20, 2014, 12:31 AM by iman baqir   [ updated Jan 20, 2014, 12:31 AM by Karwan Jacksi ]

Method                          Description and Example

charAt(index)                 The argument index is an int value and specifies a character position in the string. The first character is at position 0, the second character is at position I, and so forth. The method returns the character at the specified position. The return value is of the type char.

                                      Example:

char letter;

String name=  "Herman";

Letter = nam.charAt(3);

After this code executes, the variable letter will hold the character ‘m’.

 

 

Length()                         This method returns the number of characters in the string. The return value is of the type int.

                                      Example:

int stringSize;

String name - "Herman";

stringSize - name.length();

After this code executes, the atringSize variable will hold the value 6.

 

         

toLowerCasc()               This method returns a new string that is the lowercase equivalent of the string contained in the calling object.

Example:

String bigName = "HERMAN";

String littleName = bigNamc.toLowerCasc();

After this code executes, the object referenced by littleName will hold the string “herman”.

 

toUpperCase()               This method returns A new string that is the uppercase equivalent of the string contained in the calling object.

 

 

Example:

String littleName = "herman";

String bigName = littleName,toUpperCaae();

After this code executes, the object referenced by bigName will hold the string “HERMAN”.

Comments