Java 之字符串

本文通过一个Java程序展示了字符串的各种基本操作,包括字符串的创建、截取、拼接、获取长度及查找字符位置等。该程序使用了char数组来初始化字符串,并演示了如何通过指定起始位置和长度来截取字符串子串,还展示了字符串拼接的方法。



public class ZiFuChuan {
     public static void main(String args[])
     {
    	 char a[] = { 'g','o','o','d'};
    	 String s = new String(a);
    	 System.out.println(s);
    	 char b[] = {'n','o','b','s','t','u','d','e','n','t'};
    	 String s1 = new String(b,3,7);//取一段字符
    	 System.out.println(s1);
    	 String s2 = "are students";
    	 String s3 = new String("We");
    	 String s4 = s3 + " " + s2;//字符串的拼接
    	 System.out.println(s4);
    	 int size = s2.length();//字符串的长度
    	 System.out.println(size);
    	 int place1 = s1.indexOf("t");//字符串t在字符串s1第一次出现的位置
    	 int place2 = s1.lastIndexOf("t");//字符t在字符串s1最后一次出现的位置
    	 System.out.println(place1);
    	 System.out.println(place2);
    	 int booktime = 4;
    	 float pratice = 2.5f;
    	 System.out.println("I use"+" "+booktime+" hours reading book; "+
    	 (booktime+pratice)+ " play"+" "+"computer");
     }
}

运行结果

good
student
We are students
12
1
6
I use 4 hours reading book; 6.5 play computer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值