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;
}
}
|