String类——常见字符串操作指令

来源:互联网 发布:上海软件项目经理工资 编辑:程序博客网 时间:2024/05/21 09:10

String 方法

下面是 String 类支持的方法,更多详细,参看 Java String API 文档:

SN(序号)方法描述1char charAt(int index)
返回指定索引处的 char 值。2int compareTo(Object o)
把这个字符串和另一个对象比较。3int compareTo(String anotherString)
按字典顺序比较两个字符串。4int compareToIgnoreCase(String str)
按字典顺序比较两个字符串,不考虑大小写。5String concat(String str)
将指定字符串连接到此字符串的结尾。6boolean contentEquals(StringBuffer sb)
当且仅当字符串与指定的StringButter有相同顺序的字符时候返回真。7static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的 String。8static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的 String。9boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。10boolean equals(Object anObject)
将此字符串与指定的对象比较。11boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。12byte[] getBytes()
 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。13byte[] getBytes(String charsetName)
使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。14void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。15int hashCode()
返回此字符串的哈希码。16int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。17int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。18int indexOf(String str)
 返回指定子字符串在此字符串中第一次出现处的索引。19int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。20String intern()
 返回字符串对象的规范化表示形式。21int lastIndexOf(int ch)
 返回指定字符在此字符串中最后一次出现处的索引。22int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。23int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。24int lastIndexOf(String str, int fromIndex)
 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。25int length()
返回此字符串的长度。26boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。27boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。28boolean regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。29String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。30String replaceAll(String regex, String replacement
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。31String replaceFirst(String regex, String replacement)
 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。32String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。33String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串。34boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。35boolean startsWith(String prefix, int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始。36CharSequence subSequence(int beginIndex, int endIndex)
 返回一个新的字符序列,它是此序列的一个子序列。37String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。38String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。39char[] toCharArray()
将此字符串转换为一个新的字符数组。40String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。41String toLowerCase(Locale locale)
 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。42String toString()
 返回此对象本身(它已经是一个字符串!)。43String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。44String toUpperCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。45String trim()
返回字符串的副本,忽略前导空白和尾部空白。46static String valueOf(primitive data type x)
返回给定data type类型x参数的字符串表示形式。



/*String类适用于描述字符串事物。那么它就提供了多个方法对字符串进行操作。常见的操作有哪些?"abcd"1,获取。1.1 字符串中的包含的字符数,也就是字符串的长度。int length():获取长度。1.2 根据位置获取位置上某个字符。char charAt(int index):1.3 根据字符获取该字符在字符串中位置。int indexOf(int ch):返回的是ch在字符串中第一次出现的位置。int indexOf(int ch, int fromIndex) :从fromIndex指定位置开始,获取ch在字符串中出现的位置。int indexOf(String str):返回的是str在字符串中第一次出现的位置。int indexOf(String str, int fromIndex) :从fromIndex指定位置开始,获取str在字符串中出现的位置。int lastIndexOf(int ch) :2,判断。2.1 字符串中是否包含某一个子串。boolean contains(str):特殊之处:indexOf(str):可以索引str第一次出现位置,如果返回-1.表示该str不在字符串中存在。所以,也可以用于对指定判断是否包含。if(str.indexOf("aa")!=-1)而且该方法即可以判断,有可以获取出现的位置。2.2 字符中是否有内容。boolean isEmpty(): 原理就是判断长度是否为0. 2.3 字符串是否是以指定内容开头。boolean startsWith(str);2.4 字符串是否是以指定内容结尾。boolean endsWith(str);2.5 判断字符串内容是否相同。复写了Object类中的equals方法。boolean equals(str);2.6 判断内容是否相同,并忽略大小写。boolean equalsIgnoreCase();3,转换。3.1 将字符数组转成字符串。构造函数:String(char[])  String(char[],offset,count):将字符数组中的一部分转成字符串。静态方法:static String copyValueOf(char[]);static String copyValueOf(char[] data, int offset, int count) static String valueOf(char[]):3.2 将字符串转成字符数组。**char[] toCharArray():3.3 将字节数组转成字符串。String(byte[])String(byte[],offset,count):将字节数组中的一部分转成字符串。3.4 将字符串转成字节数组。byte[]  getBytes():3.5 将基本数据类型转成字符串。static String valueOf(int)static String valueOf(double)//3+"";//String.valueOf(3);特殊:字符串和字节数组在转换过程中,是可以指定编码表的。4,替换String replace(oldchar,newchar);5,切割String[] split(regex);6,子串。获取字符串中的一部分。String substring(begin);String substring(begin,end);7,转换,去除空格,比较。7.1 将字符串转成大写或则小写。 String toUpperCase(); String toLowerCase();7.2 将字符串两端的多个空格去除。String trim();7.3 对两个字符串进行自然顺序的比较。int compareTo(string);*/class  StringMethodDemo{public static void method_7(){String s = "    Hello Java     ";sop(s.toLowerCase());sop(s.toUpperCase());sop(s.trim());String s1 = "a1c";String s2 = "aaa";sop(s1.compareTo(s2));}public static void method_sub(){String s = "abcdef";sop(s.substring(2));//从指定位置开始到结尾。如果角标不存在,会出现字符串角标越界异常。sop(s.substring(2,4));//包含头,不包含尾。s.substring(0,s.length());}public static void  method_split(){String s = "zhagnsa,lisi,wangwu";String[] arr  = s.split(",");for(int x = 0; x<arr.length; x++){sop(arr[x]);}}public static void method_replace(){String s = "hello java";//String s1 = s.replace('q','n');//如果要替换的字符不存在,返回的还是原串。String s1 = s.replace("java","world");sop("s="+s);sop("s1="+s1);}public static void method_trans(){char[] arr = {'a','b','c','d','e','f'};String s= new String(arr,1,3);sop("s="+s);String s1 = "zxcvbnm";char[] chs = s1.toCharArray();for(int x=0; x<chs.length; x++){sop("ch="+chs[x]);}}public static void method_is(){String str = "ArrayDemo.java";//判断文件名称是否是Array单词开头。sop(str.startsWith("Array"));//判断文件名称是否是.java的文件。sop(str.endsWith(".java"));//判断文件中是否包含Demosop(str.contains(".java"));}public static void method_get(){String str = "abcdeakpf";//长度sop(str.length());//根据索引获取字符。sop(str.charAt(4));//当访问到字符串中不存在的角标时会发生StringIndexOutOfBoundsException。//根据字符获取索引sop(str.indexOf('m',3));//如果没有找到,返回-1.//反向索引一个字符出现位置。sop(str.lastIndexOf("a"));}public static void main(String[] args) {method_7();//method_trans();//method_is();//method_get();/*String s1 = "abc";String s2 = new String("abc");String s3 = "abc";System.out.println(s1==s2);System.out.println(s1==s3);*/}public static void sop(Object obj){System.out.println(obj);}}


阅读全文
0 0
原创粉丝点击