您的位置:首页 > 职场人生

黑马程序员-Scanner的使用、string类的概述和使用

2015-08-14 19:50 429 查看
1:Scanner的使用(了解)

(1)在JDK5以后出现的用于键盘录入数据的类。

(2)构造方法:

A:讲解了System.in这个东西。

它其实是标准的输入流,对应于键盘录入

B:构造方法

案例理解:

System类下有一个静态的字段:

* public static final InputStream in; 标准的输入流,对应着键盘录入。

* InputStream 是引用数据类型, in是对应的变量

* InputStream is = System.in;

* System.in是类名直接调用变量,赋值给 InputStream 的引用数据 is

* class Demo {

* public static final int x = 10;

* public static final Student s = new Student();

* }

* int y = Demo.x;

* Student s = Demo.s;

InputStream is = System.in;

Scanner(InputStream is)

参数是 InputStream 要给is 赋值

C:常用的格式

Scanner sc = new Scanner(System.in);

(3)基本方法格式:

A:hasNextXxx() 判断是否是某种类型的

B:nextXxx() 返回某种类型的元素

(4)要掌握的两个方法

A:public int nextInt()

B:public String nextLine()

(5)需要注意的小问题

A:同一个Scanner对象,先获取数值,再获取字符串会出现一个小问题。

* int – int 正常

* String – String 正常

* String – int 正常

* int – String 不正常

*

原因:如果是String – String ,输入string 下一次标记仍为string,不出问题,如果是int-string ,回车记录的是int,不符合string,而且回车符/n默认是一个字符串

B:解决方案:

a:重新定义一个Scanner对象

b:把所有的数据都用字符串获取,然后再进行相应的转换

2:String类的概述和使用(掌握)

(1)多个字符组成的一串数据。

其实它可以和字符数组进行相互转换。

(2)构造方法:

A:public String()

B:public String(byte[] bytes)//把一个字节数组转换成字符串

C:public String(byte[] bytes,int offset,int length)//把一个字节数组的一部分转换为字符串 (从指定索引开始,截取指定长度)

D:public String(char[] value) //把一个字符数组转换成字符串

E:public String(char[] value,int offset,int count) //把一个字符数组的一部分转换为字符串 (从指定索引开始,截取指定长度)

下面的这一个虽然不是构造方法,但是结果也是一个字符串对象

G:String s = “hello”;

空串与null的区别

String s = “”; String s1 = null;

举例:字符串就是羊肉串,空串就是签子,null的话就是把签子扔掉.

H.数组和字符串分别通过什么方式获长度?

数组:使用的是数组的.length属性

字符串:使用的是字符串的.length()方法

(3)字符串的特点

A:字符串一旦被赋值,就不能改变。

注意:不可以被改变说的是常量池中的值,并不是引用s(s是可以重新被赋值的)。

B:字面值作为字符串对象和通过构造方法创建对象的不同

String s = new String(“hello”);和String s = “hello”的区别?

String s = new String(“hello”) 创建了2个对象 ,存在两个”abc”字符串,一个是在常量池中,一个是在堆内存当中.

String s = “hello” 只有一个对象,在内存中只存在一个”abc”在常量池中.

(4)字符串的面试题(看程序写结果)

A:==和equals()

== 比较的是地址值 equals 比较默认是地址值,如果重写,比较的是内容,一般都重写

String s1 = new String(“hello”);

String s2 = new String(“hello”);

System.out.println(s1 == s2);// false

System.out.println(s1.equals(s2));// true

String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4);// false
System.out.println(s3.equals(s4));// true

String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);// true

B:字符串的拼接(面试)

总结:字符串如果是 变量 相加,先开空间(新的地址值),在相加

字符串如果是 常量 相加,先加,再到常量池里找对应的字符串,如何有,直接返回,没有,就创建新的地址值

String s1 = “hello”;

String s2 = “world”;

String s3 = “helloworld”;

System.out.println(s3 == s1 + s2);// false,因为变量相加,开辟新空间

System.out.println(s3.equals((s1 + s2)));// true,内容相同

System.out.println(s3 == “hello” + “world”);// true,常量相加,先加在找对应字符串

System.out.println(s3.equals(“hello” + “world”));// true,内容是相同

,

(5)字符串的功能

A:判断功能

String s = “HelloWorld”;

// boolean equals(Object obj):判断字符串的内容是否相同,区分大小写。

System.out.println(s.equals(“HelloWorld”));

System.out.println(s.equals(“helloworld”));

System.out.println(“——————–”);

// boolean equalsIgnoreCase(String str):判断字符串的内容是否相同,不区分大小写。

System.out.println(s.equalsIgnoreCase(“HelloWorld”));

System.out.println(s.equalsIgnoreCase(“helloworld”));

System.out.println(“——————–”);

// boolean contains(String str):判断字符串对象是否包含给定的字符串。

System.out.println(s.contains(“or”));

System.out.println(s.contains(“ak”));

System.out.println(“——————–”);

// boolean startsWith(String str):判断字符串对象是否以给定的字符串开始。

System.out.println(s.startsWith(“Hel”));

System.out.println(s.startsWith(“hello”));

System.out.println(“——————–”);

// boolean endsWith(String str):判断字符串对象是否以给定的字符串结束。省略不讲。

// boolean isEmpty():判断字符串对象是否为空。数据是否为空。

System.out.println(s.isEmpty());

String s2 = “”;

System.out.println(s2.isEmpty());

案例、以下程序运行会出现什么结果

String s1 = “”;

String s2 = null;

System.out.println(s1.isEmpty()); //true

System.out.println(s2.isEmpty()); //NullPointerException空指针异常

3.案例讲解

* 模拟登录,给三次机会,并提示还有几次

*

* 思路:

* A:应该已经存在有用户名和密码。用String表示就可以。

* B:键盘录入数据:用户名和密码。

* C:把数据进行比较。

* 匹配:成功

* 不匹配:失败。

* D:给三次机会,说明用循环控制。而告诉你了三次。用for循环。

* E:提示还有几次。其实很简单。

*/

public class StringTest {

public static void main(String[] args) {

// 应该已经存在有用户名和密码。用String表示就可以。

String username = “admin”;

String password = “admin”;

// 三次机会

for (int x = 0; x < 3; x++) {

// x 0,1,2

// 键盘录入数据:用户名和密码。

Scanner sc = new Scanner(System.in);

System.out.println(“请输入用户名:”);

String name = sc.nextLine();

System.out.println(“请输入密码:”);

String pwd = sc.nextLine();

// 把数据进行比较。

if (username.equals(name) && password.equals(pwd)) {

System.out.println(“恭喜你,登录成功”);

// 引入曾经的猜数字小游戏。

break;

} else {

if ((2 - x) == 0) {

System.out.println(“账号被锁定,请与林青霞联系”);

} else {

// 2,1,0

System.out.println(“登录失败,你还有” + (2 - x) + “次机会”);

}

}

}

}

}

B:获取功能

// 创建字符串对象

String s = “helloworld”;

// int length():获取字符串的长度

System.out.println(s.length());

System.out.println(“——–”);

// char charAt(int index):返回字符串中给定索引处的字符

System.out.println(s.charAt(2));

System.out.println(“——–”);

// 遍历字符串。

for (int x = 0; x < s.length(); x++) {

char ch = s.charAt(x);

System.out.println(ch);

}

System.out.println(“——–”);

// int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引

System.out.println(s.indexOf(‘l’));

int indexOf(int ch,int fromIndex):返回此字符串中第一次出现指定字符的索引,从指定的索引开始搜索。

System.out.println(s.indexOf(‘l’, 4));

System.out.println(“——–”);

// String substring(int start):截取字符串。返回从指定位置开始截取后的字符串。

//String s = “helloworld”;

System.out.println(s.substring(4));

String substring(int start,int end)截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。

System.out.println(s.substring(3, 5));//常见的方法:包左不包右。

//截取的串要和以前一样。
System.out.println(s.substring(0));
System.out.println(s.substring(0,s.length()));
System.out.println(s);

案例:*统计大写小写字符的个数

* 举例:

* Hello12345World

* 大写:2

* 小写:8

* 数字:5

*

* 思路:

* A:定义三个统计变量

* B:获取到每一个字符。遍历字符串。

* C:判断是属于哪种范围的

* 大写:65-90 ‘A’-‘Z’

* 小写:97-122 ‘a’-‘z’

* 数字:48-57 ‘0’-‘9’

* C:是哪种哪种++

*/

public class StringTest {

public static void main(String[] args) {

String s = “Hello12345World”;

// 定义三个统计变量

int bigCount = 0;

int smallCount = 0;

int numberCount = 0;

// 遍历字符串。

for (int x = 0; x < s.length(); x++) {

char ch = s.charAt(x);

if (ch >= ‘A’ && ch <= ‘Z’) {

bigCount++;

} else if (ch >= ‘a’ && ch <= ‘z’) {

smallCount++;

} else if (ch >= ‘0’ && ch <= ‘9’) {

numberCount++;

}

}

System.out.println(“大写:” + bigCount);

System.out.println(“小写:” + smallCount);

System.out.println(“数字:” + numberCount);

}

}

C:转换功能

String s = “HelloWorld”;

// byte[] getBytes():把字符串转换成字节数组。

byte[] bys = s.getBytes();

for (int x = 0; x < bys.length; x++) {

System.out.println(bys[x]);

}

System.out.println(“—————–”);

// char[] toCharArray():把字符串转换成字符数组。

char[] chs = s.toCharArray();

for (int x = 0; x < chs.length; x++) {

System.out.println(chs[x]);

}

System.out.println(“—————–”);

// static String copyValueOf(char[] chs):把字符数组转换成字符串。

char[] chs2 = { ‘a’, ‘b’, ‘c’, ‘中’, ‘国’ };

String s2 = String.copyValueOf(chs2);

System.out.println(s2);

System.out.println(“—————–”);

// static String valueOf(char[] chs):把字符数组转换成字符串。

String s3 = String.valueOf(chs2);

System.out.println(s3);

System.out.println(“—————–”);

// static String valueOf(int i):把int数据转换成字符串

int i = 100;

String s4 = String.valueOf(i);

System.out.println(s4);

System.out.println(“—————–”);

// String toLowerCase():把字符串变成小写

System.out.println(s.toLowerCase());

// String toUpperCase():把字符串变成大写

System.out.println(s.toUpperCase());

System.out.println(“—————–”);

// String concat(String str):拼接字符串。

String s5 = “hello”;

String s6 = s5 + “world”;

String s7 = s5.concat(“world”);

System.out.println(s6);

System.out.println(s7);

}

案例:需求:把字符串的首字母转成大写,其余为小写

* 举例:

* helloWorld

* 结果:

* Helloworld

*

* 思路:

* A:截取首字母。

* B:截取其他字母。

* C:把A转大写+B转小写

*/

public class StringTest {

public static void main(String[] args) {

String s = “helloWorld”;

String s1 = s.substring(0, 1);

String s2 = s.substring(1);

String s3 = s1.toUpperCase().concat(s2.toLowerCase());

System.out.println(s3);

// 链式编程:核心思想是对象调方法还是对象

String result = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());

// System.out.println(result);

}

}

D:其他功能

String replace(char oldChar, char newChar )//替换功能,用指定新的字符替换旧的字符

String s = “helloworld”;

System.out.println(s.replace(‘l’, ‘p’));

String replace(String oldString, String newString)//用指定新的字符串去替换老的字符串(新串和老串的长度可以不同)

System.out.println(s.replace(“ll”, “ak47”));

String[] split(String regex) //切割功能 以指定的字符串为标记切割字符串,返回一个字符串数组.

String ages = "20-30";
String[] strArray = ages.split("-");//
for (int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]);
}

String trim()去除字符串两端的空格

String name = ” admin hello “;

System.out.println(“” + name.trim() + ““);

int compareTo(String str)//用字典顺序比较两个字符串.如果相同返回0.

String s1 = “hello”;

String s2 = “aello”;

String s3 = “mello”;

String s4 = “hello”;

String s5 = “Hello”;

//System.out.println(‘m’+1);//109

System.out.println(s1.compareTo(s2));// 7

System.out.println(s1.compareTo(s3));// -5

System.out.println(s1.compareTo(s4));// 0

案例:大串中查找小串出现的次数

获取一个字符串中指定子串出的次数。比如说“hanbasdnbafllgnbahjnbakqqqqlnbaxi” 在这个字符串中,多有个nba?

思路回顾:

A:定义两个字符串。一个大串,一个小串。

定义一个统计变量,次数默认为0。

B:在大串中查找一个小串第一次出现的索引:indexOf()

如果存在,就返回索引。这个时候,我们把统计变量++。

如果不存在,就返回-1。这个时候,我们就直接把统计变量的值返回即可。

C;把上次查找到的小串的第一个字符的索引记录住。

然后从这个索引+小串长度处开始截取大串,重新赋值给以前的大串。

回到B。

public class StringTest2 {

public static void main(String[] args) {

String str = “hanbasdnbafllgnbahjnbakqqqqlnbaxnbai”;

String regex = “nba”;

// 功能

int count = getCount(str, regex);

System.out.println(count);

}

* 返回值:统计变量的值 int 参数列表:大串和小串

*/

public static int getCount(String maxString, String minString) {

// 定义统计变量

int count = 0;

// 在大串中查找小串一次

int index = maxString.indexOf(minString);

// 如果返回值不是-1,说明小串在大串中是存在的。

while (index != -1) {

// 统计变量++

count++;

// 把查找过的数据给截取掉,重新赋值给大串

maxString = maxString.substring(index + minString.length());

// 在新的大串中查找一次小串

index = maxString.indexOf(minString);

}

return count;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: