java中怎么计算输入字符串的长度

如题所述

通过String自带的length()方法获取字符串长度。
String a="abcdefg";//定义一个字符串
int len = a.length();//通过length获取字符串长度,这里等于7

length()该方法返回此字符串的长度。长度是等于Unicode代码单元中的字符串的数目。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-08
public class ZiFuChuanChangDuTest {
public int f1(String a){
int sum=0;
sum=a.length();
return sum;
}
public static void main(String args[]){
ZiFuChuanChangDuTest n=null;
n=new ZiFuChuanChangDuTest();
Scanner sc=new Scanner(System.in);
System.out.println("请输入字符:");
String i=sc.nextLine();
System.out.println("字符串长度为:"+n.f1(i));
}
}本回答被网友采纳
相似回答