zoukankan      html  css  js  c++  java
  • Java实用类-String类

     1 Java使用类:
     2 String类
     3      String:在java.lang包中,被final 修饰 ,不可以有子类
     4 字符串常量: "String","hello"...  
     5 声明字符串对象: String str;String string="Hello";
     6 创建字符串对象 :str=new String("this is a string!");//使用字符串常量传值 创建
     7            :str= new String(string);//使用一个已经创建的字符串 创建 字符串
     8            :str= new String(ch);//char  ch[]={'a','b','c','d','e'};//使用字符串数组进行创建,相当于"abcde"
     9            :str= new String(ch,1,3);//提取字符数组 里面的一部分字符 进行创建字符串对象
    10 注意:String str1=new String("ok"); String str2=new String("ok");
    11     str1==str2  :答案为false-->因为new  是 不同的实例 
    12     若是:str1="ok",str2="ok","ok"放在字符串常量池,
    13     str1==str2  : true--> str1和str2都是同一个引用,
    14     
    15 String 常用方法:
    16 public int length();//获取一个字符串的长度
    17 public boolean equals(String s);//比较 两个字符串的实体 '内容' 是否相等,不是比较的引用
    18 public boolean equalsIgnoreCase(String s);//忽略大小写 ,比较两个字符串的内容
    19 public boolean startsWith(String s);//判断 字符串对象的 前缀 是否是s指定的字符串
    20 public boolean endsWith(String s);//判断 字符串对象的 后缀是否是s指定的字符串
    21 public boolean regionMatches(int firstStart,String other,int otherStart,int length);    //当前字符串 从firstStart 开始 ,取长度为length 的子串 ;和另一个在字符串other ,从otherStart开始,取长度为length的字符串,看这个两个字符串是否相等
    22 public boolean regionMatches(boolean b,int firstStart,String other,int otherStart,int length);    //b=true  表示 忽略大小写 
    23 public int compareTo(String s);//字符串和s 按字典序 比较大小:=s返回0; >s返回正数;<s返回负数;
    24 public int compareIgnoreCase(String s);//忽略大小写
    25 public boolean contains(String s);//判断字符串 是否还有 字符串s
    26 public int indexOf(String s);//表示 字符串 从头开始  检索,返回字符串s首次出现的位置;没有返回-1
    27 public int indexOf(String s,int startPoint);//表示 字符串 从startPoint开始  检索,返回字符串s首次出现的位置;没有返回-1
    28 public int lastIndexOf(String s);//表示 字符串 从尾部 开始  检索,返回字符串s首次出现的位置;没有返回-1
    29 public String subString(int startPoint);//返回字符串 从startPoint开始的子串
    30 public String subString(int startPoint,int endPoint);//返回字符串 从startPoint开始到endPoint的子串
    31 public String trim();//字符串 去掉 前后 空格
    32 public boolean isEmpty();//判断字符串是否为空
    33 public String[] split(String regex);//字符串 分解成字符串数组;regex表正则表达式
    34 字符串与基本类型相互转换:
    35 字符串转化成数字:java.lang中的 Integer类 里面的
    36 public static int  parseInt(String s);// Integer.parseInt("12345");
    37 类似:
    38 public static byte parseByte(String s);
    39 public static short parseShort(String s);
    40 public static long parseLong(String s);
    41 public static float parseFloat(String s);
    42 public static double parseDouble(String s);//Double.parseDouble("123.456");-->123.456
    43 
    44 将数字 转换成 字符串:
    45 public static String valueOf(byte n);
    46 public static String valueOf(int n);//String.valueOf(1234);-->"1234"
    47 public static String valueOf(long n);
    48 public static String valueOf(float n);
    49 public static String valueOf(double n);
    50 
    51 进制表示:Long.方法
    52 public static String toBinaryString(long num);
    53 public static String toOctalString(long num);
    54 public static String toHexString(long num);
    55 public static String toString(long num,int x);//num 要表示要转换的数,x表示要转换成x进制
    56 
    57 
    58 字符串与字符数组
    59 将字符串变成字符数组: 
    60 public void getChars(int start,int end,char ch[],int offsert);
    61     //字符串 从start到end-1部分 赋给  从offset开始的字符数组 ch ;要保证能字符数组能装下
    62 将字符串 全部存放在 字符串数组中:
    63 public char[]  toCharArray();
    64 字符数组变成字符串
    65 String(char[]);//str= new String(ch);  ch[]={'a','b','c','d','e'};
    66 String(charp[],int offset,int length);str= new String(ch,1,3);
    67 
    68 字符串 与 字节数组
    69 字节数组 变成字符串
    70 String(byte[]);//使用指定的字节数组 构造成一个字符串对象
    71 String(byte[],int offset,int length);//offset:数组开始的位置
    72 字符串 变成字节数组
    73 public byte[] getBytes();//默认调用平台的字符串编码,将字符串变成一个字节数组
    74 public byte[] getBytes(String charsetName);//使用参数指定字符编码,将字符串 转换成一个字节数组
  • 相关阅读:
    48. Rotate Image
    47. Permutations II
    46. Permutations
    45. Jump Game II
    44. Wildcard Matching
    43. Multiply Strings
    42. Trapping Rain Water
    41. First Missing Positive
    40. Combination Sum II
    39. Combination Sum
  • 原文地址:https://www.cnblogs.com/NirobertEinteson/p/12013540.html
Copyright © 2011-2022 走看看