String简述

String:

  1.字符串类,java中的字符串文字都被视为字符串类的实例;

  2.字符串对象被赋值后不能更改,是不可变的;

  3.字符串使用+进行拼接操作,实际上是使用的StringBuilder或StringBuffer的append方法进行的,会创建对应的新对象;

  4.从JDK1.0版本开始有的该类;

构造方法:

  String():创建一个空字符序列;

  String(byte[] bytes,String charsetName):使用指定的字符编码,按照byte数组中的元素创建一个新的字符序列;

  String(char[] value):使用字符数组中的元素创建新的字符序列;

  String(String str):使用已存在的字符序列,创建一个一样内容的字符序列,相当于是已存在字符序列的副本;

  String(StringBuffer buffer):使用StringBuffer对象创建对应的字符序列;

  String(StringBuilder builder):使用StringBuilder对象创建对应的字符序列;

方法:

  length():获取字符串的长度;

  isEmpty():判断字符串是否为空,当且仅当字符串的长度为0时,才返回true;

  charAt(int index):获取字符串指定索引处的字符内容,如果索引值超出了字符串的长度-1,会报StringIndexOutOfBoundsException异常;

   getChars(int srcBegin,int srcEnd,char[] chars,int destBegin):将字符串中指定索引范围内的字符拷贝到字符数组中,从字符数组的指定索引处开始拷贝;

    srcBegin:字符串中字符的起始索引;

    srcEnd:字符串中字符的终止索引,起始这个位置的字符不进行拷贝,只拷贝到srcEnd-1的位置;

    chars:目标字符数组;

    destBegin:目标字符数组的起始索引;

  getBytes();获取字符串在当前系统的默认编码格式下的byte数组;

  getBytes(String charsetName):根据指定的编码格式获取字符串的byte数组;

  equals(Object object):判断字符串是否相等,当且仅当参数不为null,并且是一个String类型的对象,并且具有相同的字符序列才返回true;

  contentEquals(StringBuffer buffer):判断字符串与buffer对象的字符序列是否相等;

  equalsIgnoreCase(String str):不区分大小写的情况下判断两个字符串的字符序列是否相等;

  compareTo(String str):判断两个字符串的字符系列中对应字符的Unicode码是否相等;

  compareToIgnoreCase(String str):不区分大小写的情况下,判断两个字符串的字符系列中对应字符的Unicode码是否相等;

  startsWith(String prefix):判断字符串是否是以指定的前缀开头的;

  startsWith(String prefix, int index):从字符串的指定索引开始,判断后面的子字符串是否是以指定的前缀开头的;

  endsWith(String suffix):判断字符串是否是以指定的后缀结尾的;

  hashCode():获取字符串的哈希码值;

  endsWith(String suffix):判断字符串是否是以指定的后缀结尾的;

  hashCode():获取字符串的哈希码值;

  indexOf(int ch):获取指定的字符在字符串中第一次出现的索引;

  indexOf(String str):回去指定的子字符串在当前字符串中第一次出现的索引; 

  indexOf(int ch,int fromIndex):从字符串指定的索引开始,获取字符在字符串中第一次出现的索引;

  indexOf(String subString,int fromIndex):从指定的索引开始,获取子字符串在该字符串中第一次出现的索引;

  lastIndexOf(int ch):获取指定的字符在字符串中最后一次出现的索引;

  lastIndexOf(int ch,int fromIndex):从字符串中的指定索引处反向查找字符最后一次出现的索引;

  lastIndexOf(String subString,int fromIndex):从字符串指定的索引处反向查找子字符串最后一次出现的索引;

  substring(int fromIndex):获取字符串从指定的索引处到字符串结尾的子字符串,索引的值为0到字符串的长度处(不是字符串的长度-1处,此时,截取的子字符串为空字符串);

  substring(int fromIndex,int endIndex):获取字符串指定的索引范围内(含头不含尾)的子字符串;

  concat(String str):进行字符串拼接,返回一个新字符串;

  replace(char oldChar,char newChar):用新字符替换字符串中指定的字符;

  replace(CharSequence target,CharSequence replacement):用新的字符序列替换字符串中指定的字符序列;

  matches(String regex):判断字符串是否匹配正则表达式;

  split(String regex):使用符合正则表达式的子字符串分割字符串,返回值是一个字符串数组;

  toUpperCase():将字符串中的字符转换成对应的大写;

  toLowerCase():将字符串中的字符转换成对应的小写;

  trim():去除字符串前后的空格,返回一个新字符串;

  toCharArray():将字符串转换成字符数组;

 

  以下为静态方法,可以使用String.方法名()直接调用:

  valueOf(Object object):将对象转换成对应的字符串形式;

  valueOf(int i):将int类型的数据转换成对应的字符串形式,同比,boolean、char、double、float、long类型的数据也可以进行相同转化;

  valueOf(char[] chars):使用字符数组中的元素创建一个新的字符串;

  valueOf(char[] chars,int fromIndex,int count):从字符数组的指定索引处复制指定长度的元素创建一个新字符串;

  copyValueOf(char[] chars):等同于valueOf(char[] chars);

  copyValueOf(char[] chars,int fromIndex,int count):等同于valueOf(char[] chars,int fromIndex,int count);

 

练习代码:

package com.yg.study;

import java.io.UnsupportedEncodingException;public class StringStudy {

    public static void main(String[] args) {
        String str="abcabc123@#$";
        
        //获取字符串的长度
        int len=str.length();
        System.out.println(len);
        
        //判断字符串是否为空,当且仅当字符串的长度等于0的时候,才返回true
        boolean isEmpty=str.isEmpty();
        System.out.println(isEmpty);
        
        //返回指定索引处的字符
        char c=str.charAt(3);
        System.out.println(c);
        //System.out.println(str.charAt(9));//会报StringIndexOutOfBoundsException异常
        
        //获取指定索引范围内的字符,返回一个字符数组
        char[] chars= new char[str.length()];
        str.getChars(0, 5, chars, 3);
        System.out.println(chars);
        
        //根据平台默认的字符集获取字符串对应的byte类型的数组
        byte[] bytes=str.getBytes();
        for (byte b : bytes) {
            System.out.println(b);
        }
        
        //根据指定的字符集回去字符串对应的byte类型的数组
        try {
            byte[] bytes2=str.getBytes("utf-8");
            for (byte b : bytes2) {
                System.out.println(b);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        //判断字符串是否相等,当且仅当参数不为null,并且是一个String类型的对象,并且具有相同的字符序列才返回true
        boolean equalFlag=str.equals(new String("abc123@#$"));
        System.out.println(equalFlag);
        
        //判断内容相等
        boolean contentFlag=str.contentEquals(new StringBuffer("abc123@#$"));
        System.out.println(contentFlag);
        
        //不区分大小写判断两个字符串相等
        boolean ignoreFlag=str.equalsIgnoreCase("AbC123@#$");
        System.out.println(ignoreFlag);
        
        //比较两个字符串的大小
        int result=str.compareTo("ABC123@#$");
        System.out.println(result);
        
        //不区分大小写比较两个字符串的大小
        int result2=str.compareToIgnoreCase("ABC123@#$");
        System.out.println(result2);
        
        //判断字符串是否以指定的前缀开始
        boolean startFlag=str.startsWith("abc");
        System.out.println(startFlag);
        
        //判断字符串在指定的索引处是否以指定的前缀开始
        boolean startFlag2=str.startsWith("123", 3);
        System.out.println(startFlag2);
        
        //判断字符串是否以指定的后缀结尾
        boolean endFlag=str.endsWith("@#$%");
        System.out.println(endFlag);
        
        //获取字符串的哈希码
        int hashCode=str.hashCode();
        System.out.println(hashCode);
        
        //获取指定字符在字符串中第一次出现的索引
        int index=str.indexOf(97);
        System.out.println(index);
        
        //获取指定的字符串在当前字符串中第一次出现的索引
        int index2=str.indexOf("123");
        System.out.println(index2);
        
        //从指定的索引开始查找特定的字符第一次出现的索引
        int index3=str.indexOf(98, 2);
        System.out.println(index3);
        
        //从指定的索引开始查找指定的字符串在当前的字符串中第一次出现的索引
        int index4=str.indexOf("abc",1);
        System.out.println(index4);
        
        //查找指定的字符在字符串中最后一次出现的索引
        int index5=str.lastIndexOf(98);
        System.out.println(index5);
        
        //从指定的索引开始反向查找指定的字符在字符串中最后一次出现的索引
        int index6=str.lastIndexOf(99, 4);
        System.out.println(index6);
        
        //从指定的索引开始反向搜索指定的字符串在当前字符串中最后一次出现的索引
        int index7=str.lastIndexOf("abc", 1);
        System.out.println(index7);
        
        //从指定的索引开始截取子字符串,包含该索引处,范围为0到字符串的长度(此时为空字符串)
        String substr=str.substring(5);
        System.out.println(substr);
        
        //从指定的索引开始到指定的索引结束截取子字符串,含头不含尾
        String substr2=str.substring(5, 8);
        System.out.println(substr2);
        
        //字符串的拼接
        String newString=str.concat("啦啦啦");
        System.out.println(newString);
        
        //字符串字符替换
        String newString2=str.replace('@','^');
        System.out.println(newString2);
        
        //字符串中的子字符串替换
        String newString3=str.replace("123", "666");
        System.out.println(newString3);
        
        //字符串匹配正则表达式
        boolean matchFLag=str.matches("[a-z0-9@#$]+");
        System.out.println(matchFLag);
        
        //分割字符串
        String[] strs=str.split("c");
        for (String string : strs) {
            System.out.println(string);
        }
        
        //字符串转换成对应的大写
        String upperString="中国abc".toUpperCase();
        System.out.println(upperString);
        
        //字符串转换成对应的小写
        String lowerString="aBC测试".toLowerCase();
        System.out.println(lowerString);
        
        //去除字符串前后的空格
        String trimString="   测试  abc 哈哈     ".trim();
        System.out.println(trimString);
        
        //将字符串转换成字符数组
        char[] chars2=str.toCharArray();
        for (char d : chars2) {
            System.out.println(d);
        }
        
        //将其他内容转换成字符串
        String objString=String.valueOf(new Object());
        System.out.println(objString);
        
        String intString=String.valueOf(1181);
        System.out.println(intString);
        //相当于String.copyValueOf(char[] c)
        String charArrayString=String.valueOf(new char[] {'a','p','q','g'});
        System.out.println(charArrayString);
        
        //从字符数组的指定索引开始复制指定长度的元素创建一个新数组
        String charArrayString2=String.valueOf(new char[] {'a','p','q','g'},2,2);        
        System.out.println(charArrayString2);
        
        //从指定的下标开始拷贝指定长度的字符数组生成新的字符串,如下从下标3开始,拷贝4个字符生成字符串
        String charArrayString3=String.copyValueOf(new char[] {'a','@','b','#','%','c','d'}, 3, 4);
        System.out.println(charArrayString3);
        
        String charArrayString4=String.copyValueOf(new char[] {'a','@','b','#','%','c','d'});
        System.out.println(charArrayString4);
    }

}

 

转载于:https://www.cnblogs.com/antusheng/p/10346435.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值