String对象的简单方法(特别讲解length()方法的实现。

    1. length()  返回字符串中的字符数
    2. charAt(index)  返回字符串中指定位置的字符
    3. concat(s1)    将本字符串和字符串s1连接,返回一个新字符串
    4. toUpperCase() 返回一个新字符串,其中所有的字母大写
    5. toLowerCase() 返回一个新字符串,其中所有的字母小写
    6. trim()  返回一个新字符串,去掉两边的空白字符

  

package test2;

import java.util.Scanner;

public class String_object {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner input = new Scanner(System.in);
        
        String str = input.nextLine();
        System.out.println("length "+length_(str));
    }

    public static int length_(String str) {
        //在此处补全
                int count=0;
                while(true){
                    try{
                        str.charAt(count++);
                    }catch(Exception e){
                        count--;
                        break;
                    }
                }
                return count;
    }

}
原文地址:https://www.cnblogs.com/King-boy/p/10533200.html