获取字符串长度和字符串连接

来源:互联网 发布:打电话软件下载 编辑:程序博客网 时间:2024/05/22 13:45

获取字符串长度

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

原创粉丝点击