20175316盛茂淞 2018-2019-2 《Java程序设计》第7周学习总结

时间:2023-03-09 03:47:20
20175316盛茂淞 2018-2019-2 《Java程序设计》第7周学习总结

20175316盛茂淞 2018-2019-2 《Java程序设计》第7周学习总结

教材学习内容总结

第八章 常用实用类

一、String类

  • String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。

二、String类对象的创建

  • 字符串声明:String stringName;
  • 字符串创建:stringName = new String(字符串常量);或stringName = 字符串常量;

三、String类构造方法

  • 1、public String()

    无参构造方法,用来创建空字符串的String对象。
1 String str1 = new String();
  • 2、public String(String value)

    用已知的字符串value创建一个String对象。
1 String str2 = new String("asdf"); 2 String str3 = new String(str2);
  • 3、public String(char[] value)

    用字符数组value创建一个String对象。
1 char[] value = {"a","b","c","d"};
2 String str4 = new String(value);//相当于String str4 = new String("abcd");
  • 4、public String(char chars[], int startIndex, int numChars)

    用字符数组chars的startIndex开始的numChars个字符创建一个String对象。
1 char[] value = {"a","b","c","d"};
2 String str5 = new String(value, 1, 2);//相当于String str5 = new String("bc");
  • 5、public String(byte[] values)

    用比特数组values创建一个String对象。
1 byte[] strb = new byte[]{65,66};
2 String str6 = new String(strb);//相当于String str6 = new String("AB");

四、String类常用方法

  • 1、求字符串长度
1 String str = new String("asdfzxc");
2 int strlength = str.length();//strlength = 7
  • 2、求字符串某一位置字符
public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。

1 String str = new String("asdfzxc");
2 char ch = str.charAt(4);//ch = z
  • 3、提取子串

    用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:

    (1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。

    (2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
1 String str1 = new String("asdfzxc");
2 String str2 = str1.substring(2);//str2 = "dfzxc"
3 String str3 = str1.substring(2,5);//str3 = "dfz"
  • 4、字符串比较

    (1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。

    (2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。

    (3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。

    (4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。
1 String str1 = new String("abc");
2 String str2 = new String("ABC");
3 int a = str1.compareTo(str2);//a>0
4 int b = str1.compareToIgnoreCase(str2);//b=0
5 boolean c = str1.equals(str2);//c=false
6 boolean d = str1.equalsIgnoreCase(str2);//d=true
  • 5、字符串连接
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。

1 String str = "aa".concat("bb").concat("cc");
2 相当于String str = "aa"+"bb"+"cc";
  • 6、字符串中单个字符查找

    (1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。

    (2)public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。

    (3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。

    (4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
1 String str = "I am a good student";
2 int a = str.indexOf('a');//a = 2
3 int b = str.indexOf("good");//b = 7
4 int c = str.indexOf("w",2);//c = -1
5 int d = str.lastIndexOf("a");//d = 5
6 int e = str.lastIndexOf("a",3);//e = 2
  • 7、字符串中字符的大小写转换

    (1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串

    (2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
1 String str = new String("asDF");
2 String str1 = str.toLowerCase();//str1 = "asdf"
3 String str2 = str.toUpperCase();//str2 = "ASDF"
  • 8、字符串中字符的替换

    (1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。

    (2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。

    (3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
1 String str = "asdzxcasd";
2 String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"
3 String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
4 String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
5 String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"

正则表达式

  • Java正则表达式通过java.util.regex包下的两个类:Pattern和Matcher实现的。用法如下:

    1.首先引入java包:java.util.regex.(当然,使用java.util.regex.Matcher+java.util.regex.Pattern也是一样的。)

    2.具体使用如下:①通过Pattern对象构造一个模式,具体代码为(某个特例):Pattern p = Pattern.compile("[a-z]
    "); ② 构造一个匹配器,Matcher m = p.matcher(str); (注意:此处str为待匹配的字符串); ③进行判断,得到结果:boolean b = m.matches() 。【说明】:关于Matcher类进行匹配操作时,它提供了三种方法,这三种方法返回值均为boolean类型:匹配成功,则返回true,否则返回false。 ``` 
  • .表示任意一个字符;
  • \s表示空白字符(如:空格符、换行符、制表符、回车等);
  • \S表示非空格字符;
  • \d表示一个数字,相当于[0-9];
  • \D表示一个非数字的字符,相当于[^0-9];
  • \w表示一个单词字符,相当于[]
  • \W表示一个非单词的字符,相当于[^\w];
  • ^表示一行的开始;
  • $表示一行的结尾;
  • \b表示一个单词的边界;
  • \B表示一个非单词的边界;
  • \G表示前一个匹配结束;
  • []表示匹配方括号内的一个字符,例如:[abc](或[a,b,c])表示字母a,b,c中的任意一个;[a-zA-Z](或[a-z,A-Z])表示a-z,A-Z中的任意一个字符;
  • 表示重复0次或更多次,如a表示匹配0个或更多个a;
  • +表示重复1次或更多次,如a+表示匹配1个或多个a;
  • {n}表示重复n次,如a{4}表示匹配4个a;
  • {n,}表示重复n次或更多次,如a{4,}表示匹配至少4个a;
  • {n,m}表示重复n到m次,如a{4,10}表示匹配4-10个a。

我们约定好的正则表达式为a*b,即以a开始,b结尾的字符串,代码如下:

1 import java.util.regex.;

2

3 public class RegTest {

4 static void test()

5 {

6 Pattern p = null; // 正则表达式

7 Matcher m = null; // 操作的字符串

8 boolean b = false;

9

10 // 正则表达式表示首字母是a,中间是任意字符,结尾以b结束

11 // 不匹配的结束

12 p = Pattern.compile("a
b");

13 m = p.matcher("baaaaaab");

14 b = m.matches();

15

16 // 输出匹配结果

17 System.out.println("匹配结果:"+b);

18

19 p = Pattern.compile("a*b");

20 m = p.matcher("aaaab");

21 b = m.matches();

22

23 System.out.println("匹配结果:"+b);

24 }

25

26 public static void main(String argv[])

27 {

28 test();

29 }

30 }

输出结果为:

匹配结果:false
匹配结果:true ## 教材学习中的问题和解决过程&代码调试问题

String s = "Pretty boys and girls.";

@Test

public void testCharAt() {

assertEquals('t',s.charAt(3)); //1

assertEquals(' ',s.charAt(6)); //2

assertEquals('g',s.charAt(16)); //3

assertEquals('.',s.charAt(21)); //4

//assertEquals('e',s.charAt(3)); //5

//assertEquals(' ',s.charAt(22)); //6

}

- 将5和6所在的行注释(或删除)后,其中第4行是边界,运行结果为:
![](https://img2018.cnblogs.com/blog/1272669/201904/1272669-20190414233527514-642046143.png)
- 若是将5所在行添加到项目中,会发生错误,因为测试值和给定的值不一致,具体的运行结果为:
![](https://img2018.cnblogs.com/blog/1272669/201904/1272669-20190414233637310-1878196338.png)
- 若是将6所在行添加到项目中,会发生错误,因为数组越界,超过给定字符串的长度,具体的运行结果为:
![](https://img2018.cnblogs.com/blog/1272669/201904/1272669-20190414233858112-518576154.png) ## 代码托管
![](https://img2018.cnblogs.com/blog/1272669/201904/1272669-20190414234304654-1733029402.png) ## 心得体会 教材学习有问题先去[https://shimo.im/doc/1i1gldfsojIFH8Ip/](https://shimo.im/doc/1i1gldfsojIFH8Ip/)看看,如果别人没有提出相同问题,可以编辑文档添加,然后把自己提出的问题复制到下面: - 问题1:XXXXXX
- 问题1解决方案:XXXXXX
- 问题2:XXXXXX
- 问题2解决方案:XXXXXX
- ... ## 代码调试中的问题和解决过程
- 我慢慢的感觉学习Java已经变成了每天的一项日常,会不知不觉的在空闲的时候学习一会Java,慢慢的也体会到了Java的乐趣,我相信我会带着这种乐趣一直坚持下去。 ## 学习进度条 | | 代码行数(新增/累积)| 博客量(新增/累积)|学习时间(新增/累积)|重要成长|
| -------- | :----------------:|:----------------:|:---------------: |:-----:|
| 目标 | 5000行 | 30篇 | 400小时 | |
| 第七周 | 1500/2000 | 15/18 | 160/2000 | |