字符串处理时任何编程语言都绕不开的,且很是重要,下边直接上例子,验证其方法。java
package com.test.string; public class Test { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String s = "能够证实,字符串操做时计算机程序设计中最多见的行为。"; //字符串中字符个数 System.out.println(s.length()); //参数时一个Int索引,取出索引指定位置的字符 System.out.println(s.charAt(0)); System.out.println(s.charAt(2)); /* s.getChars(int start,int end,char c[],int offset); * 该方法的做用是将当前字符串从start到end-1位置上的字符复制到字符数组c中,并从c的offset处开始存放 */ char c [] = {'1','1','1','1','1','1','1','1','1','1'}; s.getChars(0, 4, c, 3); for(int i = 0;i<c.length;i++) { System.out.print(c[i]); } System.out.println(""); //将一个String类型的字符串中包含的字符转换成byte类型而且存入一个byte[]数组中。在java中的全部数据底层都是字节,字节数据能够存入到byte数组。 byte b[] = s.getBytes(); System.out.println(b.length); byte[] b_utf8 = s.getBytes("utf-8"); String s_utf8 = new String(b_utf8,"utf-8"); System.out.println(s_utf8); //下边这句,若是编码格式不对,则会报错 String s_gbk = new String(b_utf8,"gbk"); System.out.println(s_gbk); //将字符串转换为字符数组 char[] s_c = s.toCharArray(); System.out.println(s_c[1]); //比较两个String的内容是否相同 String equals_a = "abc"; String equals_b = "x"; System.out.println((equals_a.equals(equals_b)) ? "内容相同" : "内容不一样"); //比较两个String的内容是否相同,且忽略大小写 String equalsIgnoreCase_a = "ABC"; String equalsIgnoreCase_b = "abc"; System.out.println( (equalsIgnoreCase_a.equalsIgnoreCase(equalsIgnoreCase_b)) ? "内容相同" : "内容不一样" ); //compareTo():按词典顺序比较String内容,比较结果为负数/零/正数。 String compareTo_a = "abcd"; String compareTo_b = "abdc"; System.out.println(compareTo_a.compareTo(compareTo_b)); //若是该String对象包含参数的内容,则返回true System.out.println("abc".contains("a")); //若是该String与参数的内容彻底一致,则返回true System.out.println("abc".contentEquals("abc")); /* "".regionMatches(ignoreCase, toffset, other, ooffset, len) * ignoreCase -- 若是为 true,则比较字符时忽略大小写。 --可选项 * toffset -- 此字符串中子区域的起始偏移量。 * other -- 字符串参数。 * ooffset -- 字符串参数中子区域的起始偏移量。 * len -- 要比较的字符数。 * * 若是字符串的指定子区域匹配字符串参数的指定子区域,则返回 true;不然返回 false。是否彻底匹配或考虑大小写取决于 ignoreCase 参数。 */ System.out.println("www.baidu.com".regionMatches(true,5, "baidu", 1, 4)); String with_a = "www.baidu.com"; String with_b = "www"; String with_c = "com"; //startsWith 判断字符串with_a 是否是以字符串with_b开头 System.out.println(with_a.startsWith(with_b)); //endsWith 判断字符串with_a 是否是以字符串with_c结尾 System.out.println(with_a.endsWith(with_c)); /*判断参数在字符串中的位置,若是不存在则返回-1 * indexOf 是从前开始搜索 * lastIndexOf 是从后开始搜索 */ String index_a = "判断参数在字符串中的位置,若是不存在则返回-1"; System.out.println(index_a.indexOf(",")); System.out.println(index_a.lastIndexOf("若是")); //连接String字符串 System.out.println("字符串a".concat("字符串b")); System.out.println("字符串替换测试abcd".replace("abcd", "成功")); //大写字符改为小写 System.out.println("ABCD".toLowerCase()); //小写改为大写 System.out.println("abcd".toUpperCase()); //将String两端的空白字符删除 System.out.println(" abc dce ".trim()); //把其它类型转化成字符串 String v = String.valueOf(1234567); System.out.println(v); //从字符串中截取 指定索引做为子字符串 System.out.println("www.baidu.com".substring(4, 9)); //将字符串从正则表达式匹配的地方切开 String[] t = "aaa bbb ccc ddd".split(" "); for(String t1 : t) { System.out.println(t1); } }