String length without using length() method in java

String length without using length() method in java

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

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









Related Tutorials/Questions & Answers:

Ads