首页 技术 正文
技术 2022年11月11日
0 收藏 609 点赞 2,347 浏览 6647 个字

第三阶段 JAVA常见对象的学习

第一章 常见对象——String类

(一) String 类的概述及其构造方法

(1) 概述

多个字符组成的一串数据,例如 “abc” 也可以看成是一个字符数组。

而通过 API我们又可以知道

A:字符串字面值“abc”也可以看成是一个字符串对象

B:字符串是常量,一旦被赋值,就不能改变

(2) 构造方法

//空构造
public String()//把字节数组转换成字符串
public String(byte[] bytes)//把字节数组的一部分转换成字符串
public String(byte[] bytes,int offset,int length)//把字符数组转换成字符串
public String(char[] value)//把字符数组的一部分转换成字符串
public String(char[] value,int offset,int count)//把字符串常量值转换成字符串
public String(String original)//下面的这一个虽然不是构造方法,但是结果也是一个字符串对象
String s = "hello";

简单总结:String类的构造方法可以将 字节、字符数组、字符串常量(全部或者部分)转换为字符串类型

(3) 字符串方法

//返回此字符串的长度
public int length ();

(4)构造方法和lenght方法的小案例

//以前三个为例
public class StringDemo {
public static void main(String[] args) {
//public String():空构造
String s1 = new String();
System.out.println("s1:" + s1);
System.out.println("s1.length:" + s1.length());
System.out.println("-------------------------"); //把字节数组转换成字符串:public String(byte[] bytes)
byte[] bys = {97,98,99,100,101}; //abcde
String s2 = new String(bys);
System.out.println("s2:" + s2);
System.out.println("s2.length:" + s2.length());
System.out.println("-------------------------"); //把字节数组的一部分转换成字符串:
//public String(byte[] bytes,int offset,int length)
String s3 = new String(bys,1,3);
System.out.println("s3:" + s3);
System.out.println("s3.length:" + s3.length());
}
}
//运行结果:
s1:
s1.length:0
-------------------------
s2:abcde
s2.length:5
-------------------------
s3:bcd
s3.length:3

注:97,98,99,100,101 在ASCII码中代表abcde,不熟悉的朋友请自行查阅

(5) 经典例题(必看)

例题一:

/*
* 字符串特点:一旦被赋值,就不能改变
*/
public class StringDemo {
public static void main(String[] args) {
String s = "Hello";
s += "World";
System.out.println("s:" + s);
}
}//运行结果:
s:HelloWorld

解释:不能改变是指字符串对象本身不能改变,而不是指对象的引用不能改变,上述过程中,字符串本身的内容是没有任何变化的,而是分别创建了三块内存空间,(Hello) (World) (HelloWorld) s → Hello + World → HelloWorld 。String内容的改变实际上是通过字符串之间的拼接、断开进行的,如上例中拼接后s的引用也就指向了 拼接后的HelloWorld

总结:开发中,尽量少使用 + 进行字符串的拼接,尤其是循环内,我们更加推荐使用StringBuild、StringBuffer,此内容下一篇详细讲解。

例题二:

//两者的区别
String s = new String("hello");
String s = "hello";

前者创建了2个 (1个) 对象,后者创建了1个 (0个) 对象

下面解释中若存在情况满足则,分别为创建1个和0个对象

解释:

String s = new String("hello"); 创建实例过程

  1. 在堆中创建一个对象 “hello” (new出来的),让 s 引用这个对象

  2. 在字符串常量池中查找是否存在内容为 “hello”的字符串对象

​A:若存在,将new出的对象与字符串常量池中已存在的相联系

​B:若不存在,则在字符串常量池中创建一个内容为 "abc" 的字符串对象,并与堆中的对相联系

String s = "hello"; 创建实例过程

  1. 在字符串常量中查找是否存在内容为"hello"的字符串对象

    ​A:若存在,让s直接引用该对象

    ​B:若不存在,则直接让s引用该对象

总结:前者new一个对象,“hello”隐式创建一个对象,后者只有“hello”创建一个对象,在开发中,尽量使用 String s = "hello" 的方式,效率比另一种高。

例题三:

public class StringDemo {
public static void main(String[] args) {
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
System.out.println(s5.equals(s6));//true
}
}
//运行结果
false
true
false
true
true
true

解释: == 比较地址值是否相同、String中的equals()比较字符串内容是否一致

例题四:

public class StringDemo2 {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "World";
String s3 = "HelloWorld"; System.out.println(s3 == s1 + s2);
System.out.println(s3.equals(s1 + s2));
System.out.println(s3 == "Hello" + "World"); //重点
System.out.println(s3.equals("Hello" + "World"));
}
}
//运行结果
false
true
true
true

总结:

  1. 字符串中如果是变量相加,先开空间,再拼接
  2. 字符串中如果是字符串相加,是先加,然后在常量池中找,如果有就直接返回否则就创建

(二) String类的功能

(1) 判断功能

//比较字符串的内容是否相同,区分大小写
boolean equals(Object obj)//比较字符串的内容是否相同,不区分大小写
boolean equalsIgnoreCase(String str)//判断大字符串中是否包含小字符串
boolean contains(String str)//判断某个字符串是否以某个指定的字符串开头
boolean startsWith(String str)//判断某个字符串是否以某个指定的字符串结尾
boolean endsWith(String str)//判断字符串是否为空
boolean isEmpty()注意:
String s = “ ”; // 字符串内容为空
String s = null; // 字符串对象为空

简单模拟登录案例 (String版)

import java.util.Scanner;/*
* 模拟登陆案例,给三次机会,并且提示剩余次数
* A:定义用户名和密码(已经存在的)
* B:键盘录入用户名和密码
* C:比较用户名和密码
* D:给三次机会,用循环改进
*/public class StringDemo {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
String username = "admin";
String password = "admin888"; Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String name = sc.nextLine();
System.out.println("请输入密码");
String psw = sc.nextLine();
if (name.equals(username) && psw.equals(password)) {
System.out.println("登录成功");
} else {
if ((2 - x) == 0) {
System.out.println("你的账号已经被锁定,请与管理员联系");
} else {
System.out.println("登录失败,你还有" + (2 - x) + "次机会");
}
}
}
}
}

(2) 获取功能

//获取字符串的长度
int length()//获取指定索引的字符
char charAt(int index)//返回指定字符在此字符串中第一次出现的索引
int indexOf(int ch)
//为什么这里是int而不是char?
//原因是:‘a’和‘97’其实都能代表‘a’ int方便//返回指定字符串在此字符串中第一次出现的索引
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)

字符串中数据统计案例

import java.util.Scanner;/*
* 案例:统计一个字符串中大写字母字符,小写字母字符,数字字符出现 * 的次数
*/public class StringDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要统计的数据");
String s = sc.nextLine(); int numberCount = 0;
int smallCout = 0;
int bigCout = 0; for (int x = 0; x < s.length(); x++) {
char ch = s.charAt(x);
if (ch >= 'a' && ch <= 'z') {
smallCout++;
} else if (ch >= 'A' && ch <= 'a') {
bigCout++;
} else if (ch >= '0' && ch <= '9') {
numberCount++;
}
} System.out.println("大写字母:" + bigCout + "个");
System.out.println("小写字母:" + smallCout + "个");
System.out.println("数字:" + numberCount + "个");
}
}
//运行结果
请输入需要统计的数据
HelloWorld520
大写字母:2个
小写字母:8个
数字:3个

(3) 转换功能

//把字符串转换为字节数组
byte[] getBytes()//把字符串转换成字符数组
char[] toCharArray()//把字符数组转换成字符串
static String valueOf(char[] chs)//把int类型的数据转换成字符串
static String valueOf(int i)
//注意:String类的valueOf方法可以把任何类型的数据转换成字符串!//把字符串转换成小写
String toLowerCase()//把字符串转换成大写
String toUpperCase()//把字符串拼接
String concat(String str)

(4) 其他功能

//替换功能
String replace(char old,char new)
String replace(String old,String new)//去除字符串两端空格
String trim()//按字典比较功能
int compareTo(String str)
int compareToIgnoreCase(String str)

逆序输出字符串案例

/*
* 键盘输入 "abc"
* 输出结果 "cba"
*/import java.util.Scanner;public class StringDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String line = sc.nextLine(); char[] chs = line.toCharArray(); String result = ""; for (int x = chs.length - 1; x >= 0; x--) {
result += chs[x];
}
System.out.println("reusult:" + result);
}
}
//运行结果
请输入:
abc
reusult:cba

大串中查询小串案例

import java.util.Scanner;public class StringDemo {    public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入被统计的数据");
String maxString = sc.nextLine();
System.out.println("请输入统计关键词");
String minString = sc.nextLine();
int count = getCount(maxString, minString);
System.out.println("count:" + count);
} public static int getCount(String maxString, String minString) {
//定义一个统计变量,初始化为0
int count = 0; //先在大串中查找小串第一次出现的位置
int index = maxString.indexOf(minString); //索引不是-1,说明存在,统计变量++
while (index != -1) {
count++;
//把刚才的索引 + 小串的长度作为开始位置截取上一次的大串
//返回一个新的字符串,并把该字符串的值重新赋给大串
int startIndex = index + minString.length();
maxString = maxString.substring(startIndex);
index = maxString.indexOf(minString);
}
return count;
}
}//运行结果
请输入被统计的数据
Hello520World520
请输入统计关键词
520
count:2

结尾:

如果内容中有什么不足,或者错误的地方,欢迎大家给我留言提出意见, 蟹蟹大家 !_

如果能帮到你的话,那就来关注我吧!(系列文章均会在公众号第一时间更新)

在这里的我们素不相识,却都在为了自己的梦而努力 ❤

一个坚持推送原创Java技术的公众号:理想二旬不止

下一篇: Design HashSet
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,496
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,743
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298