String length without using length() method in java

Ads

 
 

Share on Google+Share on Google+

Naulej kumar
String length without using length() method in java
1 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners

How to count length of string without using length() method in java?

Ads
View Answers

June 28, 2012 at 5:54 PM


Here is Example:

package RoseIndia;

public class ArrayString {
    public static void main (String [] args){
        int i=0;
        String str = "Roseindia";
        char[] cArray = str.toCharArray();
        for (char c : cArray)

        {
              i++;
        }
        System.out.println("String length : " + i);
            }
        }

Output:-

String length : 9

Description: This example count the number of character into the given string without using the length() method. Store the given string str into the char array by using method toCharArray(). Now take for loop as and increment int variable i till cArray has value. Now variable i contain the length of your string


Ads









Related Tutorials/Questions & Answers: