小感慨:我就静静的写着代码玩,度过一年一度的双十一,今晚就更新进度,整理笔记。



1 package com.mon11.day10;
2
3 import static org.junit.Assert.*;
4
5 import java.util.Random;
6 import java.util.Scanner;
7
8 import org.junit.Test;
9
10 /**
11 * 类说明 :String类的用法
12 * @author 作者 : chenyanlong
13 * @version 创建时间:2017年11月10日
14 */
15 public class StringTest {
16
17 // 1.Math
18 @Test
19 public void test1() {
20 System.out.println(Math.abs(-3.5));
21 System.out.println(Math.PI);
22
23 for (int i = 0; i <= 20; i++) {
24 int random = (int) (Math.random() * 10);// 生成一个0---9的随机数
25 System.out.print(random + " ");
26 }
27 }
28
29 // 2.随机数
30 @Test
31 public void test2() {
32 Random rand = new Random();
33 for (int i = 0; i <= 20; i++) {
34 int num = rand.nextInt(5);
35 System.out.print(num + " ");
36 }
37 }
38
39 // 3.比较字符串==与equals的区别
40 @Test
41 public void test3() {
42 String a = "hello";
43 String b = "hello";
44 String c = new String("hello");
45
46 // 延伸1
47 String d = "hel";
48 String e = d + "lo";
49 String f = "lo";
50 String g = d + f;
51 String h = "hel" + "lo";
52
53 System.out.println(a == b); // true
54 System.out.println(a == c);// false
55 System.out.println(a.equals(c));// true
56
57 System.out.println("---------");
58 System.out.println(a == e);// false,因为存在的变量
59 System.out.println(a == e.intern());// true
60 System.out.println(a == g);// false
61 System.out.println(a == h);// ture
62 }
63
64 // 4.计算字符串的比较
65 @Test
66 public void test4() {
67 String a = "Hello";
68 System.out.println(a.equalsIgnoreCase("HEllo"));
69 System.out.println(a.toUpperCase());
70 System.out.println(a.toLowerCase());
71
72 // 实现注册判断两次的密码是否相等
73 System.out.println("请输入密码:root,ROoT");
74 Scanner input = new Scanner(System.in);
75 String password1 = input.nextLine();
76
77 if (password1.equalsIgnoreCase("root")) {
78 System.out.println("登录成功");
79 } else {
80 System.out.println("登录失败");
81 }
82
83 }
84
85 //5.字符串的链接(+,concat)
86 @Test
87 public void test5() {
88 String s=new String("你好, ");
89 String name=new String("张三!");
90
91 String a=s.concat(name);
92 System.out.println(a);//你好, 张三!
93
94 String b=s+name;
95 System.out.println(b); //你好, 张三!
96 }
97
98 //6.判断.java文件名是否正确
99 @Test
100 public void test6() {
101 String s=new String("hello@qq.java");
102 System.out.println(s.contains(".java"));
103 System.out.println(s.contains("@"));
104
105 int index1=s.indexOf("@");
106 int index2=s.indexOf(".");
107 if(s.contains(".java")){
108 if(index1+3==index2){
109 System.out.println("邮箱格式正确");
110 }else{
111 System.out.println("邮箱格式错误");
112 }
113 }
114 }
115
116 //7.字符串的提取substring()
117 @Test
118 public void test7() {
119 String a="hello";
120 System.out.println(a.substring(3));//提取从位置索引开始的字符串部分
121 System.out.println(a.substring(1,5));//提取beginindex和endindex之间的字符串部分
122 }
123
124 //8.字符串的拆分slip
125 @Test
126 public void test8(){
127 String values="hello hello hello";
128 //int index=indexof()
129 String[] names=values.split(" ");
130 for(int i=0;i<names.length;i++){
131 System.out.println(names[i]);
132 }
133 System.out.println("------------");
134 for(String s:names){
135 System.out.println(s);
136 }
137
138 }
139
140
141
142
143 }