java中String的21种用法

来源:互联网 发布:linux 文件夹权限介绍 编辑:程序博客网 时间:2024/04/29 23:00
构造函数必须new出来

 * public String (char[] vaue)                         将一个字符数组变成字符串(构造函数)

 * public String (char[] vaue,int offset,int count)    将制定范围内的字符数组变为字符串(构造函数)

 * public String (byte[],bytes)                        将一个byte数组变为字符串(构造函数)

 * public String (byte[],bytes,int offset,int length)  将制定范围内的byte数组变为字符串(构造函数)

 * public char[] toCharArray()                         将一个字符串变为字符数组

 * public char charAt(int index)                       从一个字符串中取出顶顶位置的字符

 * public byte[](getBytes)                             将一个字符串变成byte数组

 * public int length()                                 取得字符串长度

 * public int indexOf(String str)                      从头开始查找指定字符串位置找不到返回-1

 * public int indexOf(String  str,int fromIndex)       从指定位置查找指定字符串位置

 * public String trim()                                清除左右两端的空格

 * public String substring(int beginIndex)             从指定位置开始一直取到尾进行字符串的提取

 * public String substring(int begin,int end)          指定截取字符串的开始点和结束点

 * public String[] split(String regex)                 按照指定的字符串对字符串进行拆分

 * public String toUpperCase()                         将一个字符串全部变为大写字母

 * public String toLowerCase()                         将一个字符串全部变为小写

 * public boolean startsWith(String prefix)            判断是否以字符串开头

 * public boolean endsWith(String suffix)              判断是否以字符串结尾

 * public boolean equals(String str)                   判断两个字符串是否相等

 * public boolean equalsIgnorCase(String str)           不区分大小写比较字符串是否相等

 * public String replaceAll(String regex,String replacement)字符串替换


  例:

public class StrDemos {public static void main(String args[]){    char[] s={'g','1','c','c'};    String str=null;    str=new String(s);    System.out.println(str);//将一个字符数组变成字符串            char[]d=str.toCharArray();//将一个字符串转换为字符数组    for(int i=0;i<d.length;i++){    System.out.println(d[i]);    }    }}输出:g1ccg1cc


56 0
原创粉丝点击