java-String类

来源:互联网 发布:windows驱动开发培训 编辑:程序博客网 时间:2024/04/25 02:02

String  顾名思义 字符串,java中有封装好的字符串类。

创建 :

String s = "HelloWorld";//方法1String s = new String("HelloWorld");//方法2

String类常用方法:


package yh;public class Test {public static void main(String[] args){String s = new String("HelloWorld");System.out.println(s.length());//字符长度int index = s.indexOf("ll");//查找子串位置System.out.println("子串ll位置为:" + index);String sub = s.substring(3,5);//截取某段子串System.out.println("截取的子串为:" + sub);System.out.println("判断字符串内容是否相同:" + s.equals("HelloWorld"));System.out.println("索引为6的字符是:" + s.charAt(6));//由下标获取字符System.out.println(s.toLowerCase());//字符变小写System.out.println(s.toUpperCase());//字符变大写}}


输出:


更多方法等以后用到再更新。

0 0