A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lf900827 中级黑马   /  2015-8-28 20:13  /  1088 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

7 个回复

正序浏览
这么简单的??
回复 使用道具 举报
这是就业班面试题?!有这么简单吗?
Scanner获取.length()就好了吧?
如果要复杂点就存入字符数组,然后获取数组长度就好了
回复 使用道具 举报
这不会是面试题,太简单了
回复 使用道具 举报
package Temp;

import java.util.Scanner;

public class 字符串长度 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入你的字符串:");
                String str = sc.nextLine();
                //方式一:所谓的方法求出字符串长度底层代码这么些:
            /**
             * Returns the length of this string.
             * The length is equal to the number of <a href="Character.html#unicode">Unicode
             * code units</a> in the string.
             *
             * @return  the length of the sequence of characters represented by this
             *          object.
             */
/*            public int length() {
                return value.length;
            }
            
            
//                 The value is used for character storage.
//                                private final char value[];
            
            */
            
            
               
                System.out.println(numOfString(str));
                //方式二:直接调用String类的方法求出字符串
                System.out.println(str.length());
                //所以我认为定义一个函数求出字符串的长度是一件蛋疼事情;要说算算里面有多少个a,多少个b还好;
               
               

        }
        public static int numOfString(String str)
        {
                //定义临时数组用于存储字符串转换后的字符数组;
                char[] strArry = new char[1024];
                //转成字符数组并存入
                strArry = str.toCharArray();
                //遍历字符数组
                return strArry.length;
        }

}

回复 使用道具 举报
啊?这是面试题?
回复 使用道具 举报
.length();
回复 使用道具 举报
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(s.length());
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马