String类

import java.io.UnsupportedEncodingException;



public class StringTest {
	
	public static void main(String[] args) {
		//查看字符串是否为空,长度为0返回true,否则返回false
		String s = "";
		System.out.println(s.isEmpty());
		
		//比较两个字符串的大小
		String s1 = "abc";
		String s2 = "Abc";
		System.out.println(s2.compareTo(s1));//-32
		System.out.println(s1.compareTo(s2));//32
		System.out.println(s1.compareToIgnoreCase(s2));//0
		
		//按照字符串下标取数
		String s3 = "abcd";
		System.out.println(s3.charAt(1));
		
		//字符串的连接
		System.out.println(s1.concat(s2));
		System.out.println(s1.concat(s1).concat(s2).concat(s3));
		System.out.println(s1+s2);
		
		int a = 10;
		String s6 = a+5+"123";
		System.out.println(s6);//15abcd,a和5都是数字类型,会进行加法运算
		
		System.out.println("-------------");
		//endWWith()
		System.out.println(s3.endsWith("d"));//true
		System.out.println(s3.endsWith("cd"));//true
		//startWith()
		System.out.println(s1.startsWith("a"));//true
		
		//==
		String s7 ="abc";
		System.out.println(s1==s7);//true,地址相同
		
		//getBytes()
		String s8 = "计算机";
		byte[] b4 = null;
		byte[] b2 = s8.getBytes();
		try {
			b4 = s8.getBytes("gb2312");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(byte b3 : b4){
			System.out.println(b3);
		}
		
		//indexOf()
		System.out.println(s1.indexOf("a"));//0
		System.out.println(s1.indexOf("d"));//不存在的返回-1
		
		//lastIndexOf()
		String s9 = "abcabcabc";
		System.out.println(s9.lastIndexOf("c"));//8
		System.out.println(s9.indexOf("c"));//2,从首位开始找,返回第一个
		System.out.println(s9.indexOf('c',3));//5     从下标3开始查找,包括3
		
		//replace
		String s10 = s9.replace("a", "z");//将所有的a替换为z,s9的内容不发生变化
		System.out.println(s10);//zbczbczbc
		//replaceFirst
		System.out.println(s9.replaceFirst("a", "m"));//mbcabcabc
		
		//split,得到一个字符串数组
		String [] s11 = s9.split("");//abcabcabc
		String s12 = "abbbbbcddbbbb";
		s11 = s9.split("a");//{"","bc","bc","bc"}
		s11 = s12.split("b");//{"a","","","","","c"}空格的个数等于b的个数减去1,末尾空格都被忽略,不计入总长度
		System.out.println(s11.length);//4
		for(String ss : s11){
			System.out.println(ss);
		}
		
		//subString()
		String s13 = "abcdefghi";
		System.out.println(s13.substring(1));//bcdefghi,从下标为1开始,包括1 
		System.out.println(s13.substring(2, 3));//不包括3
		
		//输出任意一个字符串的所有子串
		System.out.println("-----");
		for(int begin = 0;begin < s13.length()-1;begin++){
			for(int end = begin+1;end<s13.length();end++){
				System.out.println(s13.substring(begin, end));
			}
		}
		System.out.println("---");
		
		//toCharArray
		char[] c = s13.toCharArray();
		for(char c2: c){
			System.out.println(c2);
		}
		
		//trim,去掉字符串开始和结尾的空格,但不能去掉中间的空格
		String s14 = "   abc   abc   ";
		System.out.println(s14.trim());//abc   abc
		
		//valueOf
		int i = 123344;
		String s15 = String.valueOf(i);//
		System.out.println(s15.length());//6,得到自然数的位数
		
		
	}

}

参考文章:http://agapple.iteye.com/blog/1111377


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值