Google+

97. Using 'length( )' method for finding the length of a string






You can use the length( ) predefined method to find out the length of a string.

Example:

String myString1 = "This is a string text";

System.out.println("The length of the text stored in myString1 object is " + myString1.length( ) );

Lets implement this on Eclipse IDE:

1. Create 'LengthMethodDemo' class under any project as shown below:



2. Save and Run the 'LengthMethodDemo' class
3. Observe that the output is displayed in the console as shown below:



Note: The space between the words is also counted by the length( ) predefined method.

Download this project:

Click here to download this project containing 'LengthMethodDemo' class file used in this post (You can download this project and import into Eclipse IDE on your machine)




Please comment below to feedback or ask questions.

How to use charAt( ) method will be explained in the next post.



No comments: