字符串

Java中的字符串

对java感兴趣的小伙伴可以看看。

一、特点

  1. 字符串是不可变的

  2. 字符串存储在内存的常量池中,常量池的串池中

  3. 使用构造方法创建的字符串会在堆内存中存储,但依然指向常量池中的内容

    String s = "abc";// 产生一个对象:若串池中没有该对象,则创建一个对象放入串池中
    String s = new String("abc");//	:产生两个对象:先在串池中寻找该对象,再以该对象为构造参数在堆中创建一个对象
    

二、常用方法

  1. length():返回此字符串的长度

    public class TestLength{
        public static void main(String[] args){
            String s = "asdasdasdasdazxcg";
            int i = s.length();
            System.out.println(i);
        }
    }
    
  2. charAt():返回字符串指定索引处的字符

    public class TestLength {
    	public static void main(String[] args) {
    		String s = "sdsA651352465s0dS534D";
    		char c = s.charAt(5);
    		System.out.println(c);
    	}
    }
    
  3. endsWith(String suffix)和startsWith(String prefix) :检查字符串是否以某个子串为开头或者结尾

    public class TestLength {
    	public static void main(String[] args) {
    		String s = "Test.java";
    		boolean b = s.endsWith(".java");
    		System.out.println(b);
    		
    		boolean c = s.startsWith("t");
    		System.out.println(c);
    	}
    }
    
  4. equals(Object anObject) :用于对比两个字符串的内容是否完全一致

    public class TestLength {
    	public static void main(String[] args) {
    		String s1 = new String("abc");
    		String s2 = new String("abc");
    		System.out.println(s1.equals(s2));
    	}
    }
    
  5. indexOf(String str)和lastIndexOf(String str):返回某个字符第一次或者最后一次出现在字符串中的下标位置

    public class TestLength {
    	public static void main(String[] args) {
    		String s = "asddfgqweeryt";
    		int i = s.indexOf("d");
    		System.out.println(i);
    		
    		int j = s.lastIndexOf("d");
    		System.out.println(j);
    	}
    }
    
  6. isEmpty():判断字符串的内容是否为空串

    public class TestLength {
    	public static void main(String[] args) {
    		String s = "";
    		System.out.println(s.isEmpty());
    	}
    }
    
  7. replace(char oldChar,char newChar)和replace(String oldString,String newString):将字符串中的某个字符/字符串替换成其他字符/字符串

    public class TestLength {
    	public static void main(String[] args) {
    		String s = "aaaabbbbbaaaaabbb";
    		String ss = s.replace('a', 'c');
    		System.out.println(ss);
    		String s2 = s.replace("aaa", "ccc");
    		System.out.println(s2);
    	}
    }
    
  8. split(String s):根据参数的字符串对源字符串进行拆分,并且清除掉参数字符串

    public class TestLength {
    	public static void main(String[] args) {
    		String ss = "zhangs-30-男-北京大兴";
    		String[] split = ss.split("-");
    		for (int i = 0; i < split.length; i++) {
    			System.out.println(split[i]);
    		}
    	}
    }
    
  9. substring(int beginIndex):返回一个子串,从某个下标开始(包括这个下标)到结尾

    public class TestLength {
    	public static void main(String[] args) {
    		String ss = "0123456789";
    		String s = ss.substring(4);
    		System.out.println(s);
    	}
    }
    
  10. substring(int beginIndex,int endIndex):返回两个下标之间的子串,包含起点位置下标的字符,不包含终点位置下标的字符

    public class TestLength {
    	public static void main(String[] args) {
    		String ss = "0123456789";
    		String s = ss.substring(4,7);
    		System.out.println(s);
    	}
    }
    
  11. toCharArray():将一个字符串转换成字符数组

    public class TestLength {
    	public static void main(String[] args) {
    		String ss = "0123456789";
    		char[] cs = ss.toCharArray();
    		for (int i = 0; i < cs.length; i++) {
    			System.out.println(cs[i]);
    		}
    	}
    }
    
  12. toLowerCase()和toUpperCase():将字符串中所有的字符替换成小写/大写

    public class TestLength {
    	public static void main(String[] args) {
    		String ss = "abcdefghijkABCDEFGHIJK";
    		String lowerCase = ss.toLowerCase();
    		System.out.println(lowerCase);
    		String upperCase = ss.toUpperCase();
    		System.out.println(upperCase);
    	}
    }
    
  13. trim():清除掉字符串前端和后端的空格/换行符

    public class TestLength {
    	public static void main(String[] args) {
    		String ss = "     abc  de   fgh  ijk      ";
    		System.out.println(ss);
    		String trim = ss.trim();
    		System.out.println(trim);
    	}
    }
    

三、可变长字符串

  1. StringBuffer:JDK1.0的,运行效率低,线程安全
  2. StringBuilder:JDK1.5的,运行效率高,线程不安全
  3. 常用方法:
    1. append():对可变长字符串进行字符串追加
    2. toString():将可变长字符串转换成字符串
package com.baizhi.TestString;

public class TestStringBufferAndBuilder {
	public static void main(String[] args) {
		TestStringBuffer.m1();
		TestStringBuffer.m2();
		TestStringBuffer.m3();
	}
}

class TestStringBuffer {
	// 使用String的字符串拼接
	public static void m1() {
		long begin = System.nanoTime();
		String s = "";
		for (char j = 'a'; j <= 'z'; j++) {
			s += j;
		}
		for (int i = 0; i < 10; i++) {
			for (char j = 'a'; j <= 'z'; j++) {
				s += j;
			}
		}
		long end = System.nanoTime();
		System.out.println(end-begin);
	}
	//使用StringBuffer进行字符串拼接
	public static void m2() {
		long begin = System.nanoTime();
		
		String s = "";
		StringBuffer sb = new StringBuffer(s);
		for (int i = 0; i < 10; i++) {
			for (char j = 'a'; j <= 'z'; j++) {
				sb.append(j);
			}
		}
		s = sb.toString();
		
		long end = System.nanoTime();
		System.out.println(end-begin);
	}
	//使用StringBuilder进行字符串拼接
	public static void m3() {
		long begin = System.nanoTime();
		
		String s = "";
		StringBuilder sb = new StringBuilder(s);
		
		for (int i = 0; i < 10; i++) {
			for (char j = 'a'; j <= 'z'; j++) {
				sb.append(j);
			}
		}
		s = sb.toString();
		
		long end = System.nanoTime();
		System.out.println(end-begin);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值