具体类容:
对于每一份文档的内容而言,它都由一下的几个部分所组成:
类的定义以及相关的继承结构
类的一些简短的说说明.
类中的成员组成
类中所提供的构造方法
类中所提供的普通方法
成员、构造方法、普通方法的详细说明
字符与字符串:很多语言之中都是利用了字符数组的概念来描述字符串的信息,这一点在String类的方法上也都有所提示
No | 方法名 | 类型 | 描述 |
1 | public String(char[] value) | 构造 | 将字符数组变为String类对象 |
2 | public String(int[] codePoints, int offset,int count) | 构造 | 将部分字符组变为String |
3 | public char charAt(int index) | 普通 | 返回索引所对应的字符 |
4 | public char[] toCharArray() | 普通 | 将字符串以字符数组的形式返回 |
范例:取出制定索引的字符
package conll;
public class TestDemo {
public static void main(String[] args) {
String str = "hello";
char c = str.charAt(0);
System.out.println(c);
}
}
程序下标都是从0开始的。
范例:字符数组与字符串的转换
package conll;
public class TestDemo {
public static void main(String[] args) {
String str = "hello";
char [] data = str.toCharArray();
for(int x=0;x<str.length();x++){
System.out.print(data[x]+"、");
}
}
}
范例:字符串转大写
package conll;
public class TestDemo {
public static void main(String[] args) {
String str = "hello";
char [] data = str.toCharArray(); //将字符串变为字符数组
for(int x=0;x<str.length();x++){
data[x]-=32;
}
System.out.println(new String(data));// 将全部的字符数组变为String
System.out.print(new String(data,1,2));
}
}
范例:给定一个字符串判定其是否由数字组成
思路:如果整个字符串要判断是不是数字无法实现,但是可以将字符串变为字符数组,然后判断每一个内容是否是数字,如果该字符的范围是在0-9之间就是数字。
package conll;
public class TestDemo {
public static void main(String[] args) {
String str = "2415416513";
if(isNumber(str)){
System.out.println("字符串由数字组成!");
} else{
System.out.println("字符串不是由数字组成!");
}
}
//判断字符串是不是由数字所组成,如果是返回true,如果不是就返回false
public static boolean isNumber(String temp){
//将字符串变为字符数组,这样就可以取出每一位字符进行判断
char [] data =temp.toCharArray();
for(int x=0;x<data.length;x++){
if(data[x]>'9'||data[x]<'0'){
return false; //后续不再怕判断
}
}
return true; //如果全部通过返回true
}
}
如果写的某一个方法的返回内容是boolean,那么就习惯性的做法是将其以“isXxxx”进行命名。
字节与字符串:
字节使用byte描述,使用字节一般主要用于数据的传输或者进行编码转换,而在String类里面就提供将字符串变为字节数组的操作,目的就是为了传输以及编码转换。
No | 方法名 | 类型 | 描述 |
1 | public String(byte[] bytes) | 构造 | 将全部的字节数组变为字符串 |
2 | public String(byte[] bytes, int offset, int length) | 构造 | 将部分字节数组变为字符串 |
3 | public byte[] getBytes() | 普通 | 将字符串变为字节数组 |
4 | public byte[] getBytes(String charsetName) throws UnsupportedEncodingException | 普通 | 进行编码转换 |
范例:观察字符串与字节数组的转换
package conll;
public class TestDemo {
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)); //将字节数组变为字符串
System.out.println(new String(data,5,5));//将部分字节数组变为字符串
}
}
现在操作的是英文字母,所以感觉与字符类似。
字符串的比较:
如果要进行字符串内容相等的判断使用equals(),但是在String类里面比较判断的不止这一个。
No | 方法名 | 类型 | 描述 |
1 | public boolean equals(String anObject) | 普通 | 区分大小写 |
2 | public boolean equalsIgnoreCase(String anotherString) | 普通 | 不区分大小写 |
3 | public int compareTo(String anotherString) | 普通 | 判断两个字符串的大小(按照字符编码比较),此方法的返回值有如下三种结果: =0:表示两个字符串的内容相等 >0: <0: |
范例:判断相等
package conll;
public class TestDemo {
public static void main(String[] args) {
String str1 = "helloworld";
String str2 ="Helloworld";
System.out.println(str1.equals(str2));
System.out.println(str1.equalsIgnoreCase(str2));
}
}
范例:观察compareTo()方法
package conll;
public class TestDemo {
public static void main(String[] args) {
String str1 = "hello";
String str2 ="Hello";
System.out.println(str1.compareTo(str2));
}
}
现在只有String 类的对象才具有大小写的关系判断
字符串查找:
从一个完整的字符串之种判断某一个字符串是否存在,这一功能可以使用如下的方法完成。
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(int ch) | 普通 | 由后向前查找指定字符串的位置,找不到返回-1 |
5 | public int indexOf(int ch, int fromIndex) | 普通 | 从指定位置由后向前查找字符串的位置,找不到返回-1 |
6 | public boolean startsWith(String prefix) | 普通 | 判断是否以指定的字符串开头 |