类String

String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。

字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享。例如:

    String str = "abc";

等效于:

 char data[] = {'a', 'b', 'c'};
     String str = new String(data);

方法用法

创建一个空的字符串
String s2=new String();
把byte数值中的整数值按照ASCII码表解析成字符串---
byte[] byte1={97,98,99,100,101,102,103,104};//byte范围是128---127
        String s3=new String(byte1);


运行结果:abcdefgh
把byte数组中的整数值,从索引2的元素开始截取3个长度
byte[] bytes2={97,98,99,100,101,102,103,104};//按照ASCII码表解析
        String s4=new String(bytes2,2,3)

运行结果:cde
把字符数组转换成字符串---
char[] char1={'a','b','c','d','e'};
        String s5=new String(char1);

运行结果:abcde
把字符数组从索引2的元素开始截取2个长度转换成字符串---
 char[] char2={'a','b','c','d','e'};
        String s6=new String(char2,2,2);

运行结果:cd
获取字符串中指定索引位置的元素
String s1="abcdefghb";
        char c=s1.charAt(2);
        System.out.println(c);

运行结果:c
字符串的拼接,把s1字符串拼接上xyz
String s1="abcdefghb";
 String s2=s1.concat("xyz");
        System.out.println(s2);

运行结果:abcdefghbxyz
判断s1里面有没有x字符

 boolean b1=s1.contains("x");
        boolean b2=s2.contains("x");
        System.out.println(b1);
        System.out.println(b2);

运行结果:false   
         true
判断b字符在s1中的第几位
String s1="abcdefghb";
int i = s1.indexOf("b");
        System.out.println(i);

运行结果:1
判断b字符在s1中最后的一位的位置
 String s1="abcdefghb";
 int b = s1.lastIndexOf("b");
        System.out.println(b);

运行结果:8
返回s1的长度
String s1="abcdefghb"; 
 int length = s1.length();//返回s1的长度
        System.out.println(length);

运行结果:9
把s1里面的b全部转换成e
String s1="abcdefghb"; 
String replace = s1.replace("b", "e");
        System.out.println(replace);

运行结果:aecdefghe
判断字符串按照ASCTT码表  byte数组
String s3="abcdefghijk";
        byte[] bytes = s3.getBytes();
        System.out.println(Arrays.toString(bytes));//数组转换成字符串

运行结果:[97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107]
把字符串 转换成 字符数组
 char[] chars = s3.toCharArray();
        System.out.println(Arrays.toString(chars));

运行结果:[a, b, c, d, e, f, g, h, i, j, k]
截取字符 从第二个开始截取到第五个,但是不包含第五个
 String s1="abcdefghb"; 
String substring = s1.substring(2, 5);
        System.out.println(substring);

运行结果:cde

按照 / 拆分字符串形成字符串数组[www, 12345, tiantang, com]

String s1="abcdefghb"; 
String s4="www/12345/tiantang/com";
        String[] split = s4.split("/");
        System.out.println(Arrays.toString(split));//数组转换成字符串


运行结果:[www, 12345, tiantang, com]
去除字符串前后空格
String s5="  奥 利 奥  ";
        System.out.println(s5);
        String trim = s5.trim();//去除字符串前后空格
        System.out.println(trim);
        String replace1 = s5.replace(" ", "");//把空格转换为空
        System.out.println(replace1);


运行结果: 奥 利 奥  
        奥 利 奥
        奥利奥
判断字符串是否以 .txt结尾
  String s6="JAVATest.txt";
        boolean b3 = s6.endsWith(".txt");
        System.out.println(b3);

运行结果:true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值