zoukankan      html  css  js  c++  java
  • Java字符串进阶

    Java字符串进阶

    前言

    最常用的对字符串操作的类有三个,分别是String,StringBuilder,StringBuffer,下面将会详细的说说这三个类......

    String

    String类代表字符串,这个是最基本的对字符串的类,这个也是使用比较多的类,这里就不再详细介绍了

    构造

    • new String(String str)
    • new String(StringBuilder str)
    • new String(StringBuffer str)
    • new String(byte[] bys,String charsetName) 通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String。

    常用方法

    • str charAt(int index) 返回指定索引处的字符
    • String concat(String str) 将指定字符串str连接到此字符串的结尾,返回连接成功后的字符,因此需要接受才能有效果
    • boolean contains(CharSequence s) 判断此字符串是否包含指定的char值序列,这里的 CharSequence是一个接口,可以直接使用它的子类作为参数(String,StringBuffer,StringBuild)
    • static String copyValueOf(char[] c) 将字符数组变成字符串并且返回
    • static String copyValueOf(char[] c,int off,int count) 将截取的字符数组变成字符串并且返回,off是开始截取的下标,count是截取的个数
    • boolean endWith(String s) 判断字符串是否是以s结尾
    • boolean equals(Object o) 用于比较
    • int indexOf(char c) 返回字符c在字符串中第一次出现的索引
    • int indexOf(char c,int fromIndex) 从指定索引处开始搜索,查找第一次出现的索引
    • int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。
    • int indexOf(String str,int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
    • boolean isEmpty()
    • int length()
    • boolean matches(String regex) 是否匹配正则表达式
    • trim() 返回字符串的副本,忽略前导空白和尾部空白。
    • String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
    • String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
    • String toUpperCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
    • String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。
    • String[] split(String regex, int limit) 根据匹配给定的正则表达式来拆分此字符串。
    • char[] toCharArray() 将此字符串转换为一个新的字符数组。
    • byte[] getBytes(Charset charset) 使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组

    注意

    • 上面的new String(byte[] bys,String charsetName)这个构造方法很重要,它可以改变字符串的编码集(和byte[] getBytes(Charset charset))配合着使用,下面我们来看一个例子,代码如下:
    
            /*
    		 * InputStreamReader实现了将字节流FileInputStream转换为字符流,然后使用转换来的字节流创建高效流,从而实现高效的读写
    		 */
    
    		/*
    		 * 编码集(我的eclipse编辑器默认的是utf-8
    		 * 下面将中文字符串的编码集变为GBK写入a.txt文件,因为a.txt默认的是utf-8的因此这里在文件中显示的是乱码
    		 * 然后我们读出来的还是GBK的,因为我们写入的是GBK编码集的,但是我的eclipse是utf-8的编码集,因此在控制台上输出的还是乱码
    		 * new String(byte[] bys,String
    		 * charsetName)使用这个构造方法将byte数组改变编码集并且转换为utf-8格式的,那么这次在控制台上输出的就不乱码了
    		 */
    
    		// 将GBK格式的中文写入a.txt文件
    		File file = new File("src/a.txt");
    		FileOutputStream fileOutputStream = new FileOutputStream(file);
    		String str = "中";
    		byte[] by = str.getBytes("GBK"); // 将字符串改为GBK编码集
    		fileOutputStream.write(by);
    		fileOutputStream.close();
    		
    		//从a.txt文件中读取中文
    		FileInputStream fileInputStream = new FileInputStream(file);
    		int len;
    		byte[] bys = new byte[4];
    		while ((len = fileInputStream.read(bys)) != -1) {
    			System.out.println(new String(bys, "GBK"));
    		}
    		fileInputStream.close();
    
    

    StringBuffer

    线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。当然最重要的一点就是线程安全,我们可以从它的源码中可以看出,对于一些操作(append,insert..)都是使用了线程控制块来实现同步,适合与多线程下的使用,源代码如下:

        public synchronized StringBuffer append(Object obj) {
            super.append(String.valueOf(obj));
            return this;
        }
    
        public synchronized StringBuffer append(String str) {
            super.append(str);
            return this;
        }
        
        public synchronized StringBuffer delete(int start, int end) {
            super.delete(start, end);
            return this;
        }
    
        /**
         * @throws StringIndexOutOfBoundsException {@inheritDoc}
         * @since      1.2
         */
        public synchronized StringBuffer deleteCharAt(int index) {
            super.deleteCharAt(index);
            return this;
        }
    

    构造

    • new StringBuffer(StringBuilder str)
    • new StringBuffer(String str)

    常用的方法

    • StringBuffer append(str)将指定类型的str追加到此字符串的后面(String,char,char[],int,double,float,long,StringBuffer,StringBuilder)
    • StringBuffer insert(int offest, str) 将指定类型的str插入到此序列中,offest表示开始插入的位置的索引,类型有 String,char,char[],int,double,float,long,StringBuffer,StringBuilder
    • String delete(int fromIndex,int endIndex) 移除此序列中的字符串并且返回新的缓冲字符串
    • StringBuffer reverse() 反转字符串
    • String substring(int start) 返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
    • String substring(int start, int end) 返回一个新的 String,它包含此序列当前所包含的字符子序列。
    • StringBuffer deleteCharAt(int index) 移除此序列指定位置的 char。
    • int length() 长度
    • String toString() 返回此序列中数据的字符串表示形式。

    StringBuilder

    建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。但是这个类不是线程安全的,只适合单线程,如果使用多线程推荐使用StringBuffer,当然使用这个也行,不过需要自己实现同步

    构造方法

    • new StringBuilder(String str)

    常用方法

    这个类的常用方法和StringBuffer的一样,这里就不再一一列举了,参照上面的即可使用

  • 相关阅读:
    键值表
    CRC校验方法
    extern 使用
    编码格式简介(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
    学习积累
    二分查找写法
    生活技术常识
    JAVA实例
    JAVA_Sprint学习(一)
    手把手使用Git?
  • 原文地址:https://www.cnblogs.com/Chenjiabing/p/7026941.html
Copyright © 2011-2022 走看看