获取字符串长度
str.length() str表示字符串对象
示例:获取字符串长度
public class Demo{
public static void main(String[] args){
//声明一个字符数组
char ch[]= {'s','t','r','i','n','g'};
String str=new String(ch);
System.out.println("str的长度:"+str.length());
}
}
输出结果:
str的长度:6
字符串连接
1. 使用+运算符
String str1="hello";
String str2="world";
String str=str1+" "+str2;
输出结果:
Str=hello world
2. 使用concat()方法
String str1="hello";
String str2="world";
String str=str1.concat(str2);
输出结果:
str=helloworld