源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java string类方法深入解析

  • 时间:2022-01-19 19:55 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java string类方法深入解析
[u]复制代码[/u] 代码如下:
import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import java.util.Locale; import java.util.Date; import java.util.regex.PatternSyntaxException; import javax.xml.crypto.Data; public class Stringxuexi {   public static void main(String[] argc)   {     //charAt(int index) 返回index处的Unicode字符     String strCom = "JAVA程序设计";     System.out.println(strCom.charAt(4));     //codePointAt(int index) 返回index处字符的Unicode编码值     strCom = "I like JAVA ,too";     System.out.println(strCom.codePointAt(8));     //codePointBefore 返回index-1处字符的Unicode编码值     System.out.println(strCom.codePointBefore(2));     //codePointCount(int beginIndex,int endIndex)方法    返回指定文本范围内Unicode代码点的数量     System.out.println(strCom.codePointCount(0, 3));         //compareTo(String str)     //如果两个字符串不同,那么他们要么在某个索引处的字符不同,要么长度不同,或者同时具备这两种情况     //如果在一个或多个索引处字符不同,假设k是这类索引的最小值,那么返回值就是这两个字符串在位置k处     //两个char值之差,如果没有字符不同的索引位置,则返回值是两个字符串长度的差     System.out.println(strCom.compareTo("I like PHP"));     System.out.println(strCom.compareTo("I like JAVA too"));     //compareToIgnoreCase(String str)    忽略大小写比较字符串大小     System.out.println(strCom.compareToIgnoreCase("I Like PHP"));     //concat(String str) 将另一字符串连接在此字符串的后面,如果参数字符串的长度为0,     //则返回此字符串,否则创建一个新的String对象     System.out.println(strCom.equals(strCom.concat("")));     System.out.println(strCom.concat(strCom));     //contains(CharSequence s)判断字符串是否包含指定的char值序列     System.out.println(strCom.contains("JAVA"));     //valueOf(char []data) 静态方法,返回包含字符数组的字符的字符串     char [] array={'山','东'};     System.out.println(String.valueOf(array));     //valueOf(char[] data,int offset,int count)返回包含字符数组从offset处开始的count个字符     //组成的字符串     System.out.println(String.valueOf(array, 0, 1));     //endwith(String suffix)测试字符串是否是指定的后缀结束     System.out.println(strCom.endsWith("JAVA"));     //equals(object obj)    如果给定的对象表示的String与此String相等,则返回true,否则false     System.out.println(strCom.equals("I like JAVA"));     //equalsIgnoreCase(String anotherString) //忽略大小写与另一字符串进行比较,注意与equals方法的参数类型不同     System.out.println(strCom.equalsIgnoreCase("I Like JAva"));     //format(String format,Object ...args)静态方法    使用指定的格式字符串和参数返回一个格式话字符串     //%d 格式化为十进制整数     //%o 格式化为八进制整数     //%x %X 格式化为十六进制整数     System.out.println(String.format("%e %x %o %d %a %% %n", 15.000,15,15,15,15.0));         //format(Locale l,String format,Object ... args)     //通过给定的特殊转换符作为参数来实现对日期和时间字符串的格式化     //%te 一个月中的某一天     //%tb 指定语言环境的月份简称     //%tB 指定语言环境的月份全称     //%tA 指定语言环境的星期几全称     //%ta 指定语言环境的星期几简称     //%tc 包括全部日期和时间信息     //%tY 4位年份     //%ty 二位年份     //%tm 月份     //%tj 一年中的第几天     //%td 一个月中的第几天     Date date = new Date();     Locale form = Locale.CHINA;     String year = String.format(form, "%tY", date);     String month    = String.format(form, "%tm", date);     String day = String.format(form, "%td", date);     System.out.println("今天是: "+ year + "年"+month+"月"+day+"日");     System.out.println(String.format(form, "%tc", date));     //byte[] getBytes() 得到字符串的byte序列        byte[] str = strCom.getBytes();     for (int i = 0;i < str.length;i++)       System.out.print(str[i]+" ");     //getBytes(Charset charset)     //getBytes(String string)     //得到编码字符集的所得字符序列     try {       str = strCom.getBytes(Charset.defaultCharset());       for (int i = 0; i < str.length; i++)         System.out.println(str[i] + " ");     } catch (UnsupportedCharsetException e) {       // TODO: handle exception       e.printStackTrace();     }     //getchars(int srcBegin,int srcEnd,char[] dst,int dstBegin)     //将字符从此字符串复制到目标字符数组     char[] dst = new char[10];     strCom.getChars(0, 10, dst, 0);     for (int i = 0; i < dst.length;i++)       System.out.print(dst[i]);     System.out.println();     //hashCode()    返回字符串的哈希码,String对象的哈希码的计算公式是     //s[0]*31^(n-1)+s[1]*31^(n-2)+...+s[n-1]     //空串的哈希码为0     System.out.println(strCom.hashCode());     //indexOf(int ch) 获取字符的第一个索引,ch是一个字符,如果没有,返回-1     System.out.println(strCom.indexOf('A'));     //indexOf(int ch,int fromIndex)    //返回从从指定的索引处开始的指定字符的索引     //fromIndex没有限制,如果为负,与0等效,如果大于等于字符串长度,则返回-1     System.out.println(strCom.indexOf('A', 9));     //indexOf(String str)     //indexOf(String str,int fromIndex)     //返回指定字符串在此字符串第一次出现处的索引     System.out.println(strCom.indexOf("JAVA"));     //intern()    返回字符串对象的规范化表示形式     //当调用intern方法时,如果池已经包含一个等于此String对象的字符串,则返回池中的字符串     //否则将此字符串对象添加到池中,并返回此String对象引用     //了解这个处理机制也可以让我们在用到字符串常量的时候了解如何节省这些字符串所占用的内存。     String strCom2 = new String("I like JAVA");     System.out.println(strCom == strCom2);     System.out.println(strCom.endsWith(strCom2));     System.out.println(strCom.compareTo(strCom2));     System.out.println(strCom.intern() == strCom2.intern());     String s1 = new String("你好,Java自由人");     String s2 = new String("你好,") + "Java自由人";     System.out.println(s1==s2);     System.out.println(s1.intern()==s2.intern());     //同indexOf,注意fromIndex 参数,是指从fromIndex处反向搜索     System.out.println(strCom.lastIndexOf('A'));     System.out.println(strCom.lastIndexOf('A',10));     System.out.println(strCom.lastIndexOf("JAVA"));     System.out.println(strCom.lastIndexOf("JAVA", 10));     //返回字符串长度     System.out.println(strCom.length());     //matchs(String regex)匹配正则表达式     try {       String regex = "1234";       System.out.println(regex.matches("//d{4}"));       System.out.println(regex.replaceAll("//d{4}", "chen"));       System.out.println(regex.replaceFirst("//d{4}", "chen"));     } catch (PatternSyntaxException e) {       // TODO: handle exception       e.printStackTrace();     }     // offsetByCodePoints(int index,int codePointOffset)     //返回从给定的index处偏移codePointOffset个代码点的索引     System.out.println(strCom.offsetByCodePoints(7, 4));     //测试两个字符串区域是否相等,第一个参数为true时表示忽略大小写     System.out.println(strCom.regionMatches(true, 0, "I lIke", 0, 3));     System.out.println(strCom.regionMatches(0, "I like", 0, 3));     System.out.println(strCom.replace('A', 'a'));     System.out.println(strCom.replace("JAVA", "PHP"));     //String[] split(String regex,int limit)     //按指定的分隔符会字符串内容分割并存放到字符串数组中,limit为控制模式应用次数     String[] info = strCom.split(" ,");     for (int i = 0; i < info.length;i++)       System.out.println(info[i]);     info    = strCom.split(" ", 2);     for (int i = 0; i < info.length;i++)       System.out.println(info[i]);     //startsWith(String prefix,int toffset)//判断是否以指定前缀开始     //toffset为负或大于字符串长度结果为false     System.out.println(strCom.startsWith("I"));     System.out.println(strCom.startsWith("I",-1));     //CharSequeuece subSequeuece(int beginIndex,int endIndex)     //返回一个新的字符序列     System.out.println(strCom.subSequence(2, 6));     //String substring(int beginindex,int endIndex)     //返回子字符串     System.out.println(strCom.substring(2));     System.out.println(strCom.substring(2, 6));     //toCharArray() 字符串变字符数组     char[] str1 = strCom.toCharArray();     for (int i = 0; i < str1.length;i++)       System.out.print(str1[i]+" ");     System.out.println();     //toLowerCase(Locale locale) 将字符串中的所有字符变成大/小写返回新的字符串     System.out.println(strCom.toLowerCase());     System.out.println(strCom.toUpperCase());     System.out.println(strCom.toUpperCase(form));     System.out.println(strCom.toLowerCase(form));     //trim()方法取出字符串的前后空白     System.out.println(("    "+strCom).trim());     //valueOf()     静态方法 实现基本数据类型转成字符串     System.out.println(String.valueOf(true));     System.out.println(String.valueOf('A'));     System.out.println(String.valueOf(12.0));   } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部