java中的字符字节与字符串

1.字符与字符串

   字符串内部包含一个字符数组,String 可以和 char[] 相互转换.
public class TestDemo {
    public static void main(String[] args) {
        String str="helloworld";
        //将获取指定位置的字符下标从0开始
        System.out.println(str.charAt(0));
        //将字符串变为字符数组
        char [] data=str.toCharArray();
        for (int i = 0; i <data.length ; i++) {
            System.out.print(data[i]+"");
        }
        System.out.println();
        //字符数组转化为字符串
        System.out.println(new String(data));//全部转换
        System.out.println(new String(data,5,4));
        //部分转换,offset代表从下标几开始,count代表数目
    }
}
运行结果:h
         helloworld
         helloworld
         worl

2.字节与字符串

   字节常用于数据传输以及编码转换的处理之中,String 也能方便的和 byte[] 相互转换.
public class TestDemo {
    public static void main(String[] args) {
        String str="helloworld";
        //String 转 byte[]
        byte []data=str.getBytes();
        for (int i = 0; i <data.length ; i++) {
            System.out.print(data[i]+" ");
        }
        System.out.println();
        //byte[] 转 String
        System.out.println(new String(data));
    }
}
运行结果:104 101 108 108 111 119 111 114 108 100 
         helloworld
         第一行打印的是第二行每一个字母所对应的ASCII值

3.字符串常见用法

3.1字符串比较

public class TestDemo {
    public static void main(String[] args) {
        String str1="hello";
        String str2="Hello";
        System.out.println(str1.equals(str2));//区分大小写的比较
        System.out.println(str1.equalsIgnoreCase(str2));//不区分大小写比较
        System.out.println("A".compareTo("a"));
        System.out.println("a".compareTo("A"));
        System.out.println("A".compareTo("A"));
        System.out.println("AB".compareTo("AC"));

    }
}
运行结果:false
         true
         -32
         32
         0
         -1

在String类中compareTo()方法是一个非常重要的方法,该方法返回一个整型,该数据会根据大小关系返回三类内容:
1). 相等:返回0.
2). 小于:返回内容小于0.
3). 大于:返回内容大于0。
compareTo()是一个可以区分大小关系的方法,字符串的比较大小规则, 总结成三个字 “字典序” 相当于判定两个字符串在一本词典的前面还是后面. 先比较第一个字符的大小(根据 unicode 的值来判定), 如果不分胜负, 就依次比较后面的内容.

3.2 字符串查找

从一个完整的字符串之中可以判断指定内容是否存在。
public class TestDemo {
    public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.contains("world"));//判断是否包含指定字符串
        //判断开头或者结尾
        System.out.println(str.startsWith("h"));//是否以指定字符串为开头
        System.out.println(str.endsWith("d"));//是否以指定字符串为结尾
        System.out.println(str.startsWith("l",2));//从指定位置开始判断是否以指定字符串开头
    }
}
运行结果:true true true true

3.3 字符串替换拆分和截取

 使用一个指定的新的字符串替换掉已有的字符串数据。
public class TestDemo {
    public static void main(String[] args) {
        String str="helloworld";
        //替换所有的指定内容
        System.out.println(str.replace("l","_"));
        //替换首个内容
        System.out.println(str.replaceFirst("l","_"));
        //由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串.
        String str2 = "hello world hello bit";
        //按照空格拆分
        String []result=str2.split(" ");
        for(String s:result){
            System.out.println(s);
        }
        //按照空格拆分成2个字符串,2代表数组长度
        String []ret=str2.split(" ",2);
        for(String s:ret){
            System.out.println(s);
        }
        //"将name=zhangsan&age=18" 字符串转换成name=zhangsan,age=18输出
        String str3 = "name=zhangsan&age=18" ;
        String[] result1 = str3.split("&") ;
        
        for (int i = 0; i < result1.length; i++) {
            String[] temp = result1[i].split("=") ;
            System.out.println(temp[0]+" = "+temp[1]);
        }
        //字符串截取
        String str4="helloworld";
        //从指定索引截取到结尾
        System.out.println(str4.substring(5));
        //截取部分内容substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标
        System.out.println(str.substring(0,5));


    }
}
运行结果:   he__owor_d
			he_loworld
			hello
			world
			hello
			bit
			hello
			world hello bit
			name = zhangsan
			age = 18
			world
			hello

3.4 字符串其他操作方法

public class TestDemo {
    public static void main(String[] args) {
        String str = " hello world " ;
        System.out.println("["+str+"]");
        System.out.println("["+str.trim()+"]");
        //trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
        //大小写转换
        String str1 = " hello%$$%@#$%world 哈哈哈 " ;
        System.out.println(str.toUpperCase());
        System.out.println(str.toLowerCase());
        //这两个函数只转换字母。
        //字符串length()
        String str2 = " hello%$$%@#$%world 哈哈哈 " ;
        System.out.println(str.length());
        //观察isEmpty()方法
        System.out.println("hello".isEmpty());
        System.out.println("".isEmpty());
        System.out.println(new String().isEmpty());
    }
}
运行结果:[ hello world ]
		 [hello world]
		 HELLO WORLD 
		 hello world 
		 13
		 false
		 true
		 true

4 StringBuffer 和 StringBuilder

任何的字符串常量都是String对象,而且String的常量一旦声明不可改变,如果改变对象内容,改变的是其引用的指向而已。通常来讲String的操作比较简单,但是由于String的不可更改特性,为了方便字符串的修改,提供StringBuffer和StringBuilder类。StringBuffer 和 StringBuilder 大部分功能是相同的.
public class TestDemo {
    public static void main(String[] args) {
       StringBuffer sb=new StringBuffer();
       sb.append("hello").append("world");
       System.out.println(sb);
      //字符串反转
       System.out.println(sb.reverse());
      //删除指定范围的数据
       System.out.println(sb.delete(5,10));
       //插入数据
       System.out.println(sb.delete(5,10).insert(0,"你好吗"));
    }
}
运行结果:helloworld
		dlrowolleh
		dlrow
		你好吗dlrow

String、StringBuffer、StringBuilder的区别:
String的内容不可修改,StringBuffer与StringBuilder的内容可以修改.
StringBuffer与StringBuilder大部分功能是相似的.
StringBuffer采用同步处理,属于线程安全操作;而StringBuilder采用异步处理,属于线程不安全操作.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值