Java Doc文档简介
将所有String类的常用方法全部记下来,包括方法名称、参数作用以及类型。
一个成熟的编程语言,除了它语法非常完善之外,也需要提供大量的开发类库,Java除了本身提供的JDK Doc之外还会有大量的。
本次操作主要是以文档的查找为主,文档只有英文或者是日文。
如果想要找到String类的说明,则通过左上角找到:java.lang.
一般文档的组成都会包含如下几个部分:
- 类的定义以及实现的父类、父接口、子类、子接口等;
- 类的使用的简短的说明;
- 成员摘要说明;
- 构造说明;
- 普通方法说明;
- 具体成员、构造、普通方法依次的完整解释。
理论上讲以后所编写的开发代码都是需要动态查询文档得到的,但是一般比较常用的类或方法需要记清楚。要记住方法的作用、返回值的意义、参数的类型、参数的意义。
字符与字符串
很多编程语言都会强调利用字符数组来描述字符串,实际上在Java中也存在类似的概念,在String类中也提供有一系列与字符操作有关的方法。
| No | 方法名称 | 类型 | |
|---|---|---|---|
| 1 | public String(char[] value) | 构造 | 将全部的字符数组作为String的内容 |
| 2 | public String(char[] value,int offset,int count) | 构造 | 将部分字符数组变成字符串 ,设置字符数组的开始索引和使用个数 |
| 3 | public char charAt(int index) | 普通 | 返回指定索引位置上的字符 |
| 4 | public char[] toCharArray() | 普通 | 将字符串以字符数组形式返回 |
范例:验证charAt()方法
public class StringDemo {
public static void main(String args[]){
String str="hello";
char c=str.charAt(0);
System.out.println(c);//h
}
}
在程序中,索引都是从0开始的。
最为关键的是实现字符串与字符数组间的互相转换。
范例:将字符串变为字符数组
public class StringDemo {
public static void main(String args[]){
String str="helloworld";
char[]data =str.toCharArray();
for(int x=0;x<data.length;x++){
System.out.print(data[x]+" ");//h e l l o w o r l d
}
}
}
当字符串变为字符数组之后,就可以针对每个字符进行操作。
范例:将字符串里的小写字母变为大写字母
- 小写字母的编码=大写字母的编码+32
public class StringDemo {
public static void main(String args[]){
String str="helloworld";
char[]data =str.toCharArray();
for(int x=0;x<data.length;x++){
data[x] -= 32;//小写变大写
}
System.out.println(new String(data));
System.out.println(new String(data,5,5));//第五个索引之后,取五个内容
}
}

下面可以利用此操作功能判断某一个字符串是否全部由数字所组成。
- 将字符串变为字符数组。就可以对每个字符进行判断;
- 判断每一个字符的范围书否是数字:‘0’~‘9’之间。
范例:实现判断
public class StringDemo {
public static void main(String args[]){
String str="123";
System.out.println(isNumber(str));//true
}
public static boolean isNumber(String temp){
char data[]=temp.toCharArray();//将字符串变为字符数组
for(int x=0;x<data.length;x++){
if(data[x]<'0'||data[x]>'9'){//不是数字
return false;
}
}
return true;
}
}
为了实现判断方便可以单独定义一个isNumber()方法,这个方法返回boolean类型,但是在Java有一种命名规范,如果方法返回的是boolean类型,建议以“isXxx()”命名。
字节与字符串
除了字符可以与字符串进行转换,字节也可以进行转换,但是这样的转换往往会出现在实际的开发之中。
| No | 方法名称 | 类型 | |
|---|---|---|---|
| 1 | public String(byte[] bytes) | 构造 | 将全部字节数组变为字符串 |
| 2 | public String(byte[] bytes,int offset,int length) | 构造 | 将部分字节数组变为字符串 |
| 3 | public byte[] getBytes() | 普通 | 将字符串变为字节数组 |
| 4 | public byte[] getBytes(StringcharsetName) throws UnsupportedEncodingException | 普通 | 编码转换 |
byte虽然范围比char小,但是byte还是可以明确的描述出字母的范围的。
范例:利用字节数组实现小写字母变大写字母的操作。
public class StringDemo {
public static void main(String args[]){
String str="helloworld";
byte data[]=str.getBytes();
for(int x=0;x<data.length;x++){
data[x]-=32;//改变编码
}
System.out.println(new String(data));//HELLOWORLD
}
}
以上只是字节使用的入门,而实际中字节需要结合IO、结合网络一起分析才能够进行。
字符串比较
之前的equals()方法区分大小写,因此在Java中还有其他方法用于字符串比较。
| No | 方法名称 | 类型 | |
|---|---|---|---|
| 1 | public boolean equals(String args[]) | 普通 | 区分大小写的比较 |
| 2 | public boolean equalsIgnoreCase(String anotherString) | 普通 | 不区分大小写的比较 |
| 3 | public int compareTo(String anotherString) | 普通 | 比较字符串大小 |
| 4 | public int compareToIgnoreCase(String str) | 普通 | 不区分大小写比较字符串大小 |
范例:观察equals()和equalsIgnoreCase()的问题
public class StringDemo {
public static void main(String args[]){
String strA="hello";
String strB="HEllo";
System.out.println(strA.equals(strB));//false
System.out.println(strA.equalsIgnoreCase(strB));//true
}
}
验证码操作中可真的部分大写或者是小写字母。
在进行字符串比较时,最为关键的一个方法是:compareTo(),这个方法本身返回一个int型数据,而这个int型数据有三种结果:大于(>0)、小于(<0)、等于(=0)
范例:比较字符串大小:compareTo() 与compareToIgnoreCase()
public class StringDemo {
public static void main(String args[]){
String strA="A";
String strB="a";
System.out.println(strA.compareTo(strB));//-32
System.out.println(strA.compareToIgnoreCase(strB));//0
}
}
compareTo(),compareToIgnoreCase():根据编码比较
利用compareTo()的返回值就可以区分大小写关系
字符串查找
从一个完整的字符串之中查找一些子字符串,实现的方法有如下集中:
| No | 方法名称 | 类型 | |
|---|---|---|---|
| 1 | public boolean contains(String s) | 普通 | 判断某一个字符串是否存在 |
| 2 | public int indexOf(String str) | 普通 | 取得某一个子字符串的索引位置,找不到返回-1 |
| 3 | public int indexOf(String str,int fromIndex | 普通 | 从指定索引位置开始检索子字符串位置,找不到返回-1 |
| 4 | public int lastIndexOf(String str) | 普通 | 从后向前查找子字符串的位置,找不到返回-1 |
| 5 | public int lastIndexOf(String str, int fromIndex) | 普通 | 从指定位置由后向前查找子字符串的位置,找不到返回-1 |
| 6 | public boolean startsWith(String prefix) | 普通 | 判断是否以某个字符开头 |
| 7 | public boolean startsWith(String prefix,int toffset) | 普通 | 从指定位置判断是否以某个字符开头 |
| 8 | public boolean endsWith(String suffix) | 普通 | 判断是否以指定字符结尾 |
如果查找中间内容,基本都使用contains()方法。
范例:使用contains()方法
public class StringDemo {
public static void main(String args[]){
String str="hello";//0:h,1:e,2:l,3:l,4:o.
if(str.contains("l")){//此方法直接返回true或false
System.out.println("已查找到!");//已查找到!
}
}
}
contains()方法虽然现在用的比较多,但是其最早是在JDK1.5之后才提供的。而在JDK1.5之前,只能够通过indexOf() 方法实现
范例:利用indexOf() 方法判断
默认情况下indexOf()都是从第一个字母开始查找,那么也可以利用其重载方法从指定索引位置查找。
public class StringDemo {
public static void main(String args[]){
String str="hello";//0:h,1:e,2:l,3:l,4:o.
System.out.println(str.indexOf("l"));//2,第一个满足的位置
System.out.println(str.indexOf("l",3));//从第二个位置开始找
System.out.println(str.lastIndexOf("l"));//从第二个位置开始找
if(str.indexOf("l")!=-1){//现在表示索引查找到了
System.out.println("内容已经查找到!");//内容已经查找到!
}
}
}

在字符串查找操作里也可以判断开头和结尾
范例:判断开头或结尾
public class StringDemo {
public static void main(String args[]){
String str="**@@hello##";
System.out.println(str.startsWith("**"));
System.out.println(str.startsWith("@@",2));
System.out.println(str.endsWith("##"));
}
}

这几个查询现在出现最多的就是contains(),startsWith(),endsWith()。
字符串截取
完整字符串中截取子字符串,方法如下:
| No | 方法名称 | 类型 | |
|---|---|---|---|
| 1 | public String substring(int beginIndex) | 普通 | 从指定索引位置截取到结尾 |
| 2 | public String substring(int beginIndex,int endIndex) | 普通 | 截取指定索引范围的字符 |
范例:观察截取操作
public class StringDemo {
public static void main(String args[]){
String str="helloworldmldn";
System.out.println(str.substring(5));//worldmldn
System.out.println(str.substring(5,10));//world
}
}
千万不要忽略,在实际中,截取操作很常用。
字符串替换
将指定字符串替换为其他内容。在String中有如下方法支持替换操作。
| No | 方法名称 | 类型 | |
|---|---|---|---|
| 1 | public String replaceAll(String regex,String replacement) | 普通 | 字符串的全部替换 |
| 2 | public String replaceFirst(String regex,String replacement) | 普通 | 替换第一个内容 |
范例:观察替换操作
public class StringDemo {
public static void main(String args[]){
String str="helloworldmldn";
System.out.println(str.replaceAll("l", "_"));//he__owor_dm_dn
System.out.println(str.replaceFirst("l", "_"));//he_loworldmldn
}
}
利用替换操作可以消除数据中的全部空格
字符串拆分
可以将一个完整的字符根据指定的内容进行拆分,拆分后的结果就是多个字符串,也就是一个字符串的对象数组。可以使用的操作方法有如下几个:
| No | 方法名称 | 类型 | |
|---|---|---|---|
| 1 | public String[] split(String regex) | 普通 | 全部拆分 |
| 2 | public String[] split(String regex,int limit) | 普通 | 部分拆分 |
范例:观察split方法的使用
public class StringDemo {
public static void main(String args[]){
String str="hello world mldn elem";
String result[]=str.split(" ");//全拆分
String result1[]=str.split(" ",2);//部分拆分
for(int x=0;x<result.length;x++){
System.out.println(result[x]);
}
System.out.println("————————————————————");
for(int x=0;x<result1.length;x++){
System.out.println(result1[x]);
}
}
}

IP地址按“.”拆,但是“.”不能够直接使用,因为它属于正则表达式的定义范畴,所以日后发现不能够拆分的数据必须进行转义,使用“\”.
范例:拆IP地址
public class StringDemo {
public static void main(String args[]){
String str="192.168.1.1";
String result[]=str.split("\\.");
for(int x=0;x<result.length;x++){
System.out.println(result[x]);
}
}
}

字符串中可能包含“|”。
public class StringDemo {
public static void main(String args[]){
String str="192|168|1|1";
String result[]=str.split("\\|");
for(int x=0;x<result.length;x++){
System.out.println(result[x]);
}
}
}
结果同上。
只要在方法中发现regex,这是正则的表达,要使用转义。
其他操作方法
| No | 方法名称 | 类型 | |
|---|---|---|---|
| 1 | public int length() | 普通 | 取得字符串长度 |
| 2 | public boolean isEmpty() | 普通 | 判断是否为空字符串(不是null) |
| 3 | public String toLowerCase() | 普通 | 转小写 |
| 4 | public String toUpperCase() | 普通 | 转大写 |
| 5 | public String trim() | 普通 | 去掉左右空格,但不去掉中间空格 |
| 6 | public String concat(String str) | 普通 | 字符串连接,与“+”一样 |
范例:计算字符串长度
public class StringDemo {
public static void main(String args[]){
String str="你是一只小猪猪,QWQ";
System.out.println(str.length());//11
}
}
因为Java使用了Unicode编码,所以中文与英文长度相同,极大地方便了开发。
String中使用的是length()方法,数组中使用的是length属性。
范例:判断是否为空字符串
public class StringDemo {
public static void main(String args[]){
String str="你是一只小猪猪,QWQ";
System.out.println(str.isEmpty());//false
System.out.println("".isEmpty());//true
}
}
范例:转大小写操作
public class StringDemo {
public static void main(String args[]){
String str="Hello World!!!(*)&&^^&*^*^*^*%$&^*(";
System.out.println(str.toLowerCase());
System.out.println(str.toUpperCase());
}
}

所有字母内容都改变了,非字母没有改变
范例:观察trim()方法
public class StringDemo {
public static void main(String args[]){
String str=" Hello World ";
System.out.println("字符串内容:【"+str+"】");
System.out.println("字符串内容:【"+str.trim()+"】");
}
}

范例:字符串连接
public class StringDemo {
public static void main(String args[]){
String str="Hello ".concat("World");
System.out.println("字符串内容:【"+str+"】");//字符串内容:【Hello World】
}
}
开发中基本不会使用其实现字符串连接,都会使用“+”。
遗憾的是在String类设计中没有提供类似于数据库中的initcap()函数功能,实现首字母大写。
范例:实现首字母大写
public class StringDemo {
public static void main(String args[]){
String att="name";
System.out.println(initcap(att));//Name
}
public static String initcap(String str){
if(str==null||str.length()==0){
return str;
}
return str.substring(0,1).toUpperCase()+str.substring(1);
}
}
在进行原理分析时使用
以上所有方法需要都记住。
364

被折叠的 条评论
为什么被折叠?



