API学习String字符串

来源:互联网 发布:g41g42编程方法 编辑:程序博客网 时间:2024/06/06 00:45
package com.wonders.week01.bzl;import java.io.UnsupportedEncodingException;/** * JDK1.7 * String * (1)java.lang.Object * (2)String是final修饰的类 * (3)String类代表的是字符串,所有在java中的字符串,比如"abc",都是String类的实例。 * (4)字符串是常量,当他们被创建后他们的值是无法改变的.StringBuffer支持可变的字符串。因为字符串对象是它们分享不可变的。 * 例如:String str = "abc"; * char[] data = {'a','b','c'};String str = new String(data); * (5)下面这些例子是如何使用String字符串的。 * System.out.println("abc"); * String cde = "cde"; * System.out.println("abc"+cde); * String c = "abc".substring(2,3); * String d = cde.substring(1, 2); * (6)类String包含了检查序列中单个字符的方法,比如:截取字符串,搜索字符串,比较字符串,创建一个所有字符串转为大写或者小写的复制。 * (7)用例映射是基于字符类指定的Unicode标准版本。 * (8)Java语言对于字符串的连接有一种特殊的支持,可以采用操作符+,还有把其他的对象转为字符串。字符串连接的实现是通过StringBuffer或者 * StringBuilder来实现的。他们具有append方法。字符串的转换可以通过toString方法。Object类所定义的该方法.toString方法被在JAVA * 中的所有类继承了。 * (9)除非另外注明,将null参数传递给这个类中的构造函数或方法将导致抛出一个NullPointerException * (10)字符串表示utf - 16格式的字符串,其中补充字符由代理对表示(参见字符类中的Unicode字符表示以获取更多信息).索引值指的是char代码单元,因此一个补充字符在字符串中使用两个位置。 * (11)String类提供了处理Unicode的方法 * @author liyongyong * */public class StringTest {    public static void main(String[] args) {        String str1 = "abcdefg";        //获取字符串的长度        int length = str1.length();        System.out.println("字符串的长度是 : " + length);        //如果字符串长度为0,则返回true,否则返回false        boolean flag = str1.isEmpty();        System.out.println("字符串是否为空 : " + flag);        //返回指定位置上的字符,索引值从0开始,到该字符序列长度减1为止。        char value = str1.charAt(0);        System.out.println("str1位置为0上的字符是 : " + value);        //返回指定索引的字符编码点,索引值是从0到字符长度-1        //如果给定索引中指定的char值处于高代理范围,则以下索引小于该字符串的长度,        //而在下面的索引中char值处于低代理范围,则返回对应于此代理对的补充代码点。否则,将返回给定索引中的char值        int codePoint = str1.codePointAt(1);        System.out.println("codePoint的返回值是 : " + codePoint);         //返回指定位置的之前的字符编码点,index的范围是从0到字符串长度Length        int codePointBefore = str1.codePointBefore(3);        System.out.println("codePointBefore的返回值是 : " + codePointBefore);        //返回该字符串指定文本范围内的Unicode代码点的数量。        //如果第一个参数是负数或者第二个参数小于第一个参数则会抛出空指针异常        int  codePointCount = str1.codePointCount(1, 3);        System.out.println("codePointCount value  : " + codePointCount);        //返回该字符串中由codePointOffset代码点对给定索引偏移的索引。索引和codePointOffset给出的文本范围内的未成对代理数作为一个代码点        int offsetCodePoint = str1.offsetByCodePoints(1, 4);        System.out.println(offsetCodePoint);        //将此字符串中的字符 复制到目标字符数组中第一个被复制的字符是索引srcBegin;要复制的最后一个字符是索引srcend - 1(因此要复制的字符总数是srcend - srcbegin)。        //将字符复制到dst的子数组中,从索引dstBegin开始,以索引结束        char dst[] = {'a','b','c','d','e','f','j','k'};        str1.getChars(1,4,dst,2);        //将此字符串编码为使用命名字符集的字节序列,将结果存储为一个新的字节数组        //当这个字符串不能以指定的字符集进行解码时此时这个方法的行为就是未明确的。        //当需要对编码过程进行更多的控制时,应该使用CharsetEncoder类。        try {            byte[] b = str1.getBytes("utf-8");            for(byte a:b){                System.out.print(a + "  ");            }        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        System.out.println();        //把这个字符串与一个StringBuffer对象比较,如果且仅当此字符串表示与指定的StringBuffer相同的字符序列时,结果是true。        //此方法在StringBuffer上同步        boolean content = str1.contentEquals("abcdefg");        System.out.println("是否相等 " + content);        //将此字符串与另一个字符串进行比较,忽略了字符大小写考虑。        //如果两个字符串长度相同,并且两个字符串中对应的字符是相等的,那么两个字符串就被认为是相等的        //其中如果符合下面任意的一条规则就可以说明两个字符串是相等的:        //(1)两个字符串通过==操作符,得出的结果是true        //(2)对每一个字符串使用方法 Character.toUpperCase(char),得到了相同的结果        //(3)对每一个字符串使用方法 Character.toLowerCase(char),得到了相同的结果        boolean ignore = str1.equalsIgnoreCase("ABCDEFG");        System.out.println("忽略掉大小写是否相等 : " + ignore);        //比较两个字符串,比较每个字符串中的每个字符的Unicode值,用此字符串对象表示的字符序列与参数字符串表示的字符序列进行了比较        //如果该字符串对象的参数字符串在前,那么会返回一个负数。        //如果字符串对象和参数字符串保持一致,则会返回一个正数。        //如果字符串相等则结果返回0,compareTo方法返回0只在使用equals方法比较两个对象相等的情况下返回。        //这就是字典顺序的定义。如果两个字符串是不同的,那么它们在某个索引上有不同的字符,这是两个字符串的有效索引,或者它们的长度是不同的,或者两者都有        //如果在一个或多个索引位置有不同的字符,那么让k为最小的这样的索引        //然后,在位置k上的字符具有较小值的字符串,如通过使用<操作符>来确定的,在其他字符串之前,它将在字典中使用在这种情况下,        //compareTo返回两个字符串中位置k的两个字符值的差值,值就是: this.charAt(k)-anotherString.charAt(k)        //如果没有它们不同的索引位置,那么更短的字符串字典法在更长的字符串之前。在这种情况下,compareTo返回字符串不同的长度,this.length()-anotherString.length()        int compare = str1.compareTo("a");        System.out.println(compare);        int compare2 = str1.compareTo("hijklmn");        System.out.println(compare2);        //排除大小写,如果指定字符串大于当前字符串返回负数,如果等于等返回0,如果小于则返回正数        int result = str1.compareToIgnoreCase("ABCDEFG");        System.out.println(result);        int result1 = str1.compareToIgnoreCase("bcdefgh");        System.out.println(result1);        //如果传入的字符串部分与指定的字符串参数的部分保持一致则返回true,否则返回false        //参数1:子串在字符串中开始的位置        //参数2    字符串参数        //参数3    字符串参数的开始位置        //参数4  要比较的字符串数量        boolean flag1 =  str1.regionMatches(0, "abcde", 0, 5);        System.out.println(flag1);        boolean flag2 =  str1.regionMatches(0, "abcdf", 0, 5);        System.out.println(flag2);        //如果传入的字符串部分与指定的字符串参数的部分保持一致则返回true,否则返回false        //参数0: 是否忽略大小写        //参数1:子串在字符串中开始的位置        //参数2    字符串参数        //参数3    字符串参数的开始位置        //参数4  要比较的字符串数量        boolean flag3 =  str1.regionMatches(true,0, "ABCDE", 0, 5);        System.out.println(flag3);        boolean flag4 =  str1.regionMatches(false,0, "ABCDE", 0, 5);        System.out.println(flag4);        //测试是否字符串是按照指定的索引前缀开始的        //如果参数表示的字符序列是该对象的子字符串的前缀,则从索引toffset开始就返回true;否则返回false。        //如果toffset为负或大于该字符串对象的长度,则为false;否则结果与表达式的结果相同this.substring(toffset).startsWith(prefix)        //参数1:要开始的字符串        //参数2:从这个字符串哪个位置开始寻找该子串        boolean start = str1.startsWith("ab", 0);        System.out.println(start);        boolean start2 = str1.startsWith("ab", 7);        System.out.println(start2);        //是否字符串以指定的字符开始        //参数:指定的字符或者字符序列        //返回值:如果参数所表示的字符序列是由该字符串表示的字符序列的前缀返回true;否则返回false。        //如果参数是空字符串或等于由equals(object)方法确定的字符串对象,则返回true        boolean startWith = str1.startsWith("a");        System.out.println(startWith);        boolean startWith2 = str1.startsWith("b");        System.out.println(startWith2);        //测试是否该字符串以指定的后缀结束        //参数:指定的结束后缀        //返回值:如果参数所表示的字符序列是由该对象表示的字符序列的后缀,则返回true;否则返回false。        //如果参数是空字符串,或者等于由equals(object)方法所确定的字符串对象,那么结果将是正确的        boolean end = str1.endsWith("s");        System.out.println(end);        boolean end1 = str1.endsWith("g");        System.out.println(end1);        //返回字符串的哈希值,如果是空字符串则返回的哈希值为0        int hashValue = str1.hashCode();        System.out.println(hashValue);        int hashValue2 = "".hashCode();        System.out.println(hashValue2);        //返回指定字符第一次出现的字符串中的索引,如果字符串中不存在,则返回-1。        //如果一个具有值ch的字符出现在由该字符串对象表示的字符序列中,那么第一个的索引将返回。对于ch的值的范围从0到0xFFFF(包含),其中k是最小的值        // this.charAt(k) == chf返回true,对于ch的其他值,k的最小值如下:this.codePointAt(k) == ch返回true,对于其他的情况,在字符串中没有出现这种字符,就会返回-1.        //参数:指定字符的unicode值        int index = str1.indexOf(97);        System.out.println(index);        //返回指定字符最后一次出现的字符串中的索引,如果字符串中不存在,则返回-1。        //如果一个具有值ch的字符出现在由该字符串对象表示的字符序列中,那么最后一个的索引将返回。对于ch的值的范围从0到0xFFFF(包含),其中k是最小的值        // this.charAt(k) == chf返回true,对于ch的其他值,k的最小值如下:this.codePointAt(k) == ch返回true,对于其他的情况,在字符串中没有出现这种字符,就会返回-1.        //参数:指定字符的unicode值        int last = str1.lastIndexOf(97);        System.out.println(last);        //返回字符串中该字符第一次出现的索引,该索引要大于或者等于指定索引,如果不存在则返回-1        //参数1:指定的搜索字符序列的unicode值        //参数2:需要从该位置开始搜索        int index1 = "abbca".indexOf(97, 2);        System.out.println(index1);        //返回字符串中该字符最后一次出现的索引,该索引要小于或者等于指定索引,如果不存在则返回-1        //参数1:指定的搜索字符序列的unicode值        //参数2:需要从该位置开始搜索        int index2 = "abcdeagda".lastIndexOf(97, 8);        System.out.println(index2);        //指定的子字符串的第一次出现的索引,如果没有出现,则是- 1。        //参数:要搜索的字符串        int index3 = "abcdeagda".indexOf("b");        System.out.println(index3);        //指定的子字符串的最后一次出现的索引,如果没有出现,则是- 1。        //参数:要搜索的字符串        int index4 = "abacs".lastIndexOf("a");        System.out.println(index4);        //指定子字符串的第一次出现的索引,从指定的索引开始,如果没有这样的情况,则为- 1        //参数1:要搜索的子字符串        //参数2:索引从哪个开始搜索        int index5 = "absdfcbs".indexOf("b", 3);        System.out.println(index5);        int index6 = "abcdefghijhgf".lastIndexOf("h", 9);        System.out.println(index6);        //返回当前字符串的一个子串,新的字符串是由开始位置的字符到该字符串的末尾,子串包含开始位置的索引        //参数:指定开始的索引        //异常:如果指定的开始索引是负数或者大于字符串的长度,则会抛出异常IndexOutOfBoundsException        String  str2= str1.substring(2);        System.out.println(str2);        //返回当前字符串的一个子集。        //子字符串是从开始指定的位置开始到结束的位置为止.        //新的字符串长度是结束索引-开始索引        //子字符串是包含开始索引位置的字符,不包含结束索引位置的字符        //异常:开始索引为负数或者结束索引大于字符串长度或者是开始索引大于结束索引就会抛出IndexOutOfBoundsException        String str3 = str1.substring(2, 5);        System.out.println(str3);        //返回指定的子序列,包含开始不包含结束        //异常:如果开始或者结束索引为负数,如果结束索引大于序列的长度,或者开始索引大于结束索引则会抛出IndexOutOfBoundsException         CharSequence sequence = str1.subSequence(2, 5);        System.out.println(sequence);        //返回的是对字符串进行拼接的结果,参数字符串在最后进行拼接        String str4 = str1.concat("wonders");        System.out.println(str4);        //字符替换        //参数1:要替换掉的字符        //参数2:替换掉老字符的新字符        String newStr = str1.replace('a', '1');        System.out.println(newStr);        //如果字符串和正则表达式所匹配,则返回true,否则返回false        //参数是一个正则表达式字符串,如果正则表达式语法无效就会抛出PatternSyntaxException         boolean exp = str1.matches("[0-9]*");        System.out.println(exp);        //如果且仅当此字符串包含指定的字符序列时,返回true.        //如果参数是个null,就会抛出异常NullPointException        boolean contains = str1.contains("a");        System.out.println(contains);        //替换此字符串的第一个子字符串,该字符串匹配给定替换的给定正则表达式        //参数1:匹配该字符串的正则表达式        //参数2:要替换为第一个匹配的字符串        String str7 = str1.replaceFirst("[0-9][a-zA-Z]", "a");        System.out.println(str7);        //替换掉字符串中所有出现的符合正则表达式匹配的字符序列        String str6 = str1.replaceAll("[a-zA-Z]", "%");        System.out.println(str6);        //用指定的文字替换序列替换字符串的每个子字符串,以匹配文字目标序列。替换从字符串的开头一直到结尾        //如果两个参数只要有一个为null,就会抛出异常NullPointerException        String str8 = "aaa".replace("aa", "b");        System.out.println(str8);        //返回值是:由在给定正则表达式的匹配项下拆分此字符串计算的字符串数组        String[] str9 = "a1b2c3d4".split("[0-9]");        for (int i = 0; i < str9.length; i++) {            System.out.print( str9[i] + "   ");        }        System.out.println();        //返回值是:由在给定正则表达式的匹配项下拆分此字符串计算的字符串数组        String[] s9 = "a1b2c3d4".split("[0-9]",4);        for (int i = 0; i < s9.length; i++) {            System.out.print( s9[i] + " ");        }        System.out.println();        //转为大写字母        String string = "abcd".toUpperCase();        System.out.println(string);        //转为小写字母        String s1 = "ACSD".toLowerCase();        System.out.println(s1);        //去掉字符串的前后空格        String s3 = " a b c ".trim();        System.out.println(s3);        //将字符串转为字符数组        char[] ch = str1.toCharArray();        for (int i = 0; i < ch.length; i++) {            System.out.print( ch[i] + "     ");        }        System.out.println();        //返回该参数的字符串表示形式        //参数的类型:对象类型,字符数组,char,double,long,float,int,boolean        String s4 = String.valueOf(12.3);        System.out.println(s4);    }}

这里写图片描述
这里写图片描述
这里写图片描述

原创粉丝点击