黑马程序员——String类

来源:互联网 发布:淘宝怎么添加客服号 编辑:程序博客网 时间:2024/06/01 03:57

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

String类

1、字符串概念
          由多个字符组成的一串数据。
2、构造方法
          创建字符串对象的几种方式
String s = new String();
通过字节数组创建字符串对象
String s = new String(byte[] bys);  
通过字节数组创建字符串对象常用
String s = new String(byte[] bys,int index, int length)
通过字节数组一部分创建字符串对象
String s = new String(char[] chs)
通过字符数组创建字符串对象常用
String s = new String(char[] chs, int index, int length);
通过字符数组一部分创建字符串对象
String s = new String(String); 
通过给构造方法传入字符串创建字符字符串对象
String s = “helloworld”
直接给字符串对象赋值常用


 
3、字符串的特点及常见面试题
1、特点及注意事项
          字符串一旦被赋值,就不能改变。
          注意:字符串的值不能改变,引用是可以改变的。


               a:String s = new String("hello")和String s = "hello"的区别。
                          答:new String(“hello”)在内存中创建了1个或两个对象,为什么..
                                   “hello”在内存中创建了0个或一个对象,为什么…
               b:请写出结果:
                    String s1 = new String("hello");
                    String s2 = new String("hello");
                    System.out.println(s1==s2);
                    System.out.println(s1.equals(s2));


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


                    String s5 = "hello";
                    String s6 = "hello";
                    System.out.println(s5==s6);
                    System.out.println(s5.equals(s6));
             c : ""和null的区别
                 最本质的区别是否在内存中开辟内存空间,"'会开辟内存空间,而null不会,在开发的时候要养成良好的习惯用null


4、String类——成员方法
1、判断功能
boolean equals(Object obj)
判断字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str)
判断字符串的内容是否相同,忽略大小写
boolean contains(String str)
判断字符串对象是否包含给定的字符串
boolean startsWith(String str)
判断字符串对象是否是以给定的字符串开始
boolean endsWith(String str)
判断字符串对象是否是以给定的字符串结束
boolean isEmpty()
判断字符串对象是否为空,注意是数据


2、获取功能
int length()
获取字符串长度
char charAt(int index)
返回字符串中给定索引处的字符
int indexOf(int ch)
返回指定字符在此字符串中第一次出现的索引
int indexOf(String str)
返回指定字符串在此字符串中第一次出现的索引
int indexOf(int ch,int fromIndex)
返回指定字符在此字符串中第一次出现的索引,从指定位置开始
int indexOf(String str,int fromIndex)
返回指定字符串在此字符串中第一次出现的索引,从指定位置开始
String substring(int start)
截取字符串,返回从指定位置开始截取后的字符串,原字符串长度不变
String substring(int start,int end)
截取字符串,返回从指定位置开始到指定位置结束,截取后的字符串原字符串长度不变
//如果start=0,end=该字符串的长度,那么返回的是原对象,否则返回新对象


3、转换功能
byte[] getBytes()
把字符串转换成字节数组
char[] toCharArray()
把字符串转换成字符数组
static String copyValueOf(char[] chs)
把字符数组转换成字符串
底层调用new String(char[] chs)
static String valueOf(char[] chs)
把字符数组转换成字符串
底层调用new String(char[] chs)
static String valueOf(任意类型 变量名)
把任意类型转换成字符串
String toLowerCase()
把字符变成小写原字符串不变
String toUpperCase()
把字符编程大写原字符串不变
String concat(String str)
字符串链接原字符串不变
 
4、其他功能
String replace(char oldChar,char newChar)
替换功能原字符串不变
String replace(String oldString,String newString)
替换功能原字符串不变
String[] split(String str)
切割功能原字符串不变
字符串的split方法中的字符串
String trim()
去除字符串两端空格原字符串长度不变
int compareTo(String str)
字典顺序比较功能
int compareToIgnoreCase(String str)
字典顺序比较功能

代码体现

模拟用户登录系统

package cn.String;


import java.util.Scanner;


/**
 * 模拟用户登录 通过比较输入的用户密码来判断是否输入正确,三次机会,输入正确则进入猜数字小游戏

 *输入失败会有提示

 *
 */
public class UserLogin {
private String user;
private String password;


public UserLogin() {


}


public UserLogin(String user, String password) {
this.user = user;
this.password = password;


}


public String getUser() {
return user;
}


public void setUser(String user) {
this.user = user;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public void userLogin(UserLogin ul) {
for (int i = 0; i < 3; i++) {
System.out.println("输入");
Scanner sc = new Scanner(System.in);
String user = sc.nextLine();
if (ul.user.equals(user)) {
System.out.println("输入");
Scanner sc1 = new Scanner(System.in);
String password = sc1.nextLine();
if (ul.password.equals(password) && ul.user.equals(user)) {
System.out.println("成功");
guessNumberGame();
break;
} else {
if (i == 2) {
System.out.println("再见");
} else {
System.out.println("错误" + (i + 1) + "次");
}
}
} else {
if (i == 2) {
System.out.println("再见");
} else {
System.out.println("错误" + (i + 1) + "次");
}
}
}


}


/**
* 猜数字小游戏 用户登录成功后, 随机产生一个数字 接收用户接盘录入 返回结果 不对 则重新输入 10次不对 gameover
* */
private void guessNumberGame() {
int num = (int) (Math.random() * 100);
Scanner sc = new Scanner(System.in);
b: System.out.println("请输入");
int count = 10;
int number = 0;
a: while (true) {
if (num != (number = sc.nextInt())) {
count--;
if (num > number && count != 0) {
System.out.println("小了" + "还有" + count + "次机会");
} else if (num < number && count != 0) {
System.out.println("大了" + "还有" + count + "次机会");
}
/*
* sc = new Scanner(System.in); number = sc.nextInt();
*/
if (count == 0) {
System.out.println("gameover,是否再来?");
sc = new Scanner(System.in);
if (sc.nextInt() == 1) {
count = 10;
System.out.println("请输入:");


} else {
return;
}
}
} else {
System.out.println("恭喜,是否继续");
sc = new Scanner(System.in);
if (sc.nextInt() == 1) {
count = 0;
} else {
break a;
}


}


}
}
}

package cn.String;


public class LoginTest {
public static void main(String[] args) {

//给构造函数传入用户名和密码
UserLogin ul = new UserLogin("asdfasdf","qwerqwer");
ul.userLogin(ul);

}
}

将字符串首字母大写其余小写

public class StringDemo {
public static void main(String[] args) {
String s = "asdfsdSDfasdsdfsdf";
String s1 =s.substring(0,1);
String s2 = s.substring(1,s.length());
s =s1.toUpperCase().concat(s2.toLowerCase());
System.out.println(s);
}
}

大串中截取小串

public class BigstringSub {
public static void main(String[] args) {
String bigs = "aaawwdfgdrtdhaaaasdfasfaaaaaasdfsfaaaa";
String smalls ="aaa";
// 定义计数器记录小串出现的次数
int count  = 0;
// 定义角标获取字符串出现的位置
int index =0;
// 当大串长度小于小串时停止
while(bigs.length()>=smalls.length()){
index =bigs.indexOf(smalls);
// 以第一次出现的位置加上小串长度为起始,大串长度为终点来截取大串
bigs = bigs.substring((index+smalls.length()),bigs.length());
// 计数器加1
count++;
}
System.out.println(count);
}
}


总结:String类是一个比较特殊的类,它被final修饰,一但被创建,就无法被更改,但是可以修改它的引用,虽然看似是改变了String的值,但是实际上他只是在常量池中又开辟了一块空间,并把这块空间的地址值传给了原字符串的引用,此时因为引用的改变,导致了字符串内容的改变,但是原来的字符串并没有被改变,StringBuffer和StringBuilder为字符串缓冲区,一个是同步的另一个不同步,他们具有长度可变,可以同时操作多个数据类型,通过toString传化成字符串的功能,一般使用StringBuilder,因为效率高

0 0