JAVATM- The Beginnings

String Operations

A string can be viewed as a series of characters beginning at index zero. The String class provides several methods which perform useful string operations.

Section 1 - Obtaining the Length of a String

     

The string “Hello” contains five characters indexed from zero through four.

We can use the String class’s length method to return the number of characters in the string.

        System.out.println(s.length());         // Prints 5

                String s1 = "";

                System.out.println(s1.length());        // Prints 0

                String s2 = " World";

                System.out.println(s2.length());        // Prints 6

                String s3 = s + s2;;

                System.out.println(s3.length());        // Prints 11

Section 2 - Retrieving the Character at an Index in a String Object

The charAt method returns the character located at an index in a string. The value returned is of primitive type char. It takes an int argument which specifies the index.

char charAt(int pos)

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.



Back to ralphlecessi.com