一、String类:
1.String 类代表字符串。Java 程序中的所有字符串字面值(如"abc" )都作为此类的实例实现。
2.字符串是常量;它们的值在创建之后不能更改。
3.String 对象是不可变的,所以可以共享。
一.String类的构造方法:
1).public String():默认构造方法,没有任何参数
2).参数为字节数组:
String(byte[]bytes);
String(byte[] bytes,int startIndex,intlength)
3).参数为字符数组:
String(char[]value);
String(char[] value,intstartIndex,int length)
4).参数为字符串
String(String str):
1.判断定义为String类型的s1和s2是否相等
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
2.下面这句话在内存中创建了几个对象?
String s1 = new String("abc"); 2次 一次在堆内存中new开辟了个空间 另一次在方法区创建了"abc"
3.判断定义为String类型的s1和s2是否相等
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2); 不等于 这个比较地址了
System.out.println(s1.equals(s2)); 等于 这个是比较内容了
4.判断定义为String类型的s1和s2是否相等
String s1 = "a" + "b"+ "c";
String s2 = "abc";
System.out.println(s1 == s2); 等于 俩的地址相同
System.out.println(s1.equals(s2)); 等于 内容也相同
5.判断定义为String类型的s1和s2是否相等
String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2); 不等于 因为地址不一样
System.out.println(s3.equals(s2)); 等于 内容相同
1,判断
1.1boolean equals(Object); //判断传入的字符串是否与调用的字符串字符序列是否相同,相同就返回true否则false
1.2 boolean equalsIgnoreCase(string); //判断传入的字符串是否与调用的字符串字符序列是否相同,不区分大小写,相同就返回true否则false
1.3 boolean contains(string); //判断传入的字符串是否被调用的字符串包含
1.4 boolean startsWith(string); //判断调用的字符串是否以传入的字符串开头
1.5 boolean endsWith(string); //判断调用的字符串是否以传入的字符串结尾
1.6 boolean isEmpty(); //判断字符串是否为空
2,获取
2.1 int length(); //获取字符串的长度
2.2 char charAt(index); //通过索引获取对应的字符
2.3 int indexOf(int ch); //通过传入int数或者是字符找对应索引
int idnexOf(int ch,fromIndex); //在指定fromIndex的位置查找传入的字符
2.4 int indexOf(string str); //通过传入字符串查找字符串所对应的索引
int idnexOf(string str,fromIndex); //通过指定fromIndex的位置查找传入的字符串
2.5 int lastIndexOf(ch); //通过传入的字符从后向前找字符的索引值,把从后向前第一次找到的索引值返回
int lastIndexOf(ch,fromIndex): //通过指定fromIndex的位置,从后向前查找字符,把从后向前第一次找到的索引值返回
2.6 int lastIndexOf(string); //通过传入的字符串,从后向前查找,将第一次找到字符串中第一个字符的索引返回
int lastIndexOf(string,fromIndex): //通过指定fromIndex的位置,从后向前查找对应字符串,将第一次找到字符串中第一个字符的索引返回
2.7 String substring(start); //通过传入的索引值开始向后截取,截取的是索引到length
String substring(start,end); //通过传入的两个索引值截取,有开始有结尾,包含头不包含尾
3,转换
3.1 byte[] getBytes(); //编码,让计算机看的懂的,用默认的编码表,将字符串转换成字节数组
byte[] getBytes(String) //用指定的编码表进行编码
3.2 char[] toCharArray(); //将字符串转换成字符数组
3.3 static String copyValueOf(char[]); //将字符数组转换成字符串
static String copyValueOf(char[] data, int offset, int count);//将字符数组转换字符串,通过offset开始,截取count个
3.4 static String valueOf(char[]); //将字符数组转换成字符串
static String valueOf(char[] data, int offset, int count);//将字符数组转换字符串,通过offset开始,截取count个
3.5 static String valueOf(int); //将一个int数转换成字符串
static String valueOf(double); //同上 是double
static String valueOf(boolean); //同上 是boolean
...
3.6 static String valueOf(object);
和object.toString():结果是一样的。
3.7 String toLowerCase(): //将字符串全部转换为小写
String toUpperCase(): //将字符串全班转换为大写
3.8"abc".concat("kk"); //将两个字符串相连接,产生新的字符串
4,替换。
4.1 String replace(oldChar,newChar); //将newChar替换OldChar,如果OldChar不存在,原字符串直接赋值给替换后字符串
4.2 String replace(string,string);
5,切割。
String[] split(regex); //通过regex切割字符串,切割后会产生一个字符串数组
String s = "金三胖 郭美美 李天一";
String[] arr = s.split(" ");
6,去除字符串两空格。
String trim();
7,比较
String str = "ab";
String str1 = "bc";
int num = str.compareTo(str1); //如果str比str1大的话,返回的正数
byte[] arr = {97,98,99};
String str = new String(byte[]) //解码,让我们看的懂的,通过默认的编码表,将字节数组转换成字符串
String(byte[], String) //解码,通过指定的编码表,将字节数组转换成字符串
String(byte[],int offset, int length, String)//解码,截取字节数组,offset是开始索引,length是截取的长度
1.创建对象
使用构造函数Scanner(InputStream)传入一个输入流, 该Scanner就可以读取数据了System.in
2.读取各种类型的数据
nextInt() 可以读取一个int
nextLine() 读取一行字符串
3.关闭问题
使用结束后要调用close()方法释放资源
- public class Test1 {
- public static void main(String[] args) {
- String str1 = new String();
- System.out.println("String str1 = new String():");
- System.out.println("str1 == null : " + (str1 == null));//str1是空引用么? //false
- System.out.println("str1.length():" + str1.length());//str1中存储的字符串的长度? //0
- System.out.println("str1.equals(\"\"):" + str1.equals(""));//str1中是空字符串么?//true
- System.out.println("---------------------");
- /*
- * 参数为字节数组:
- String(byte[] bytes);
- String(byte[] bytes,int startIndex,int length)
- */
- byte[] bArray = {97,98,99,100};
- String str2 = new String(bArray);//使用byte数组构造一个String,如果byte数组都是整数,对应0-127就是ASCII码表的值
- System.out.println("str2 = " + str2);
- String str3 = new String(bArray,1,3);//用bArray来构造,从bArray的索引1开始,取2个
- // String str3 = new String(bArray,1,4);// java.lang.StringIndexOutOfBoundsException:(运行时异常)
- System.out.println("str3 = " + str3);
- /*
- * 参数为字符数组:
- String(char[] value);
- String(char[] value,int startIndex,int length)
- */
- char[] cArray = {'[','a','b',',','c','d',']'};
- String str4 = new String(cArray);
- System.out.println("str4 = " + str4);
- String str5 = new String(cArray,1,5);
- System.out.println("str5 = " + str5);
- /*
- * 用一个字符串构造一个字符串:
- * String(String str):
- */
- String str6 = new String("abc");
- System.out.println("str6 = " + str6);
- }
- }
- public class Test {
- public static void main(String[] args) {
- //在下面的过程中,产生了几个String对象空间
- //这个过程中,先后产生了三个对象空间:"hello","world","helloworld"
- //这个过程中,更改是str1的引用,
- String str1 = "hello";
- str1 = str1 + "world";
- System.out.println(str1);
- System.out.println("---------------------");
- //字符串的字面量
- String str2 = "ab";//先在常量池中找有没有"ab",如果没有,建立空间,返回引用;
- String str3 = "ab";//先在常量池中找有没有"ab",如果有,直接返回"ab"的引用;
- System.out.println(str2 == str3);//判断两个引用是否相等:true
- System.out.println(str2.equals(str3));//判断里面存储的字符串是否相同:true
- System.out.println("---------------------");
- String str4 = "a";
- String str5 = "b";
- String str6 = "ab";
- String str7 = str4 + str5;//"ab"//运算符的操作数有一个是变量,结果就是一个新字符串;
- //str7 = "a" + str5;//运算符的操作数有一个是变量,结果就是一个新字符串;
- //str7 = str4 + "b";//运算符的操作数有一个是变量,结果就是一个新字符串;
- String str8 = "a" + "b";//运算符的操作数都是字符串常量,这个结果可以确定,会先在常量池中找
- System.out.println("str6 == str7 : " + (str6 == str7));//false
- System.out.println("str6 == str8 : " + (str6 == str8));//true
- System.out.println("---------------------");
- //字符串的new 操作符
- String str9 = new String("ab");
- String str10 = new String("ab");
- System.out.println("str9 == str10 : " + str9 == str10);//肯定不等false
- System.out.println("str9.equals(str10) : " + str9.equals(str10));//肯定是true
- String str11 = "ab";
- System.out.println("str9 == str11 : " + str9 == str11);//false
- }
- }
- public class Test {
- public static void main(String[] args) {
- //假设用户已有的登录名和密码:admin 和 admin
- String loginName = "admin";
- String loginPwd = "admin";
- Scanner sc = new Scanner(System.in);
- int maxCount = 3;//最多3次机会
- while(maxCount > 0){
- System.out.print("请输入登录名:");
- String uName = sc.next();
- System.out.print("请输入登录密码:");
- String uPwd = sc.next();
- if(uName.equals(loginName) &&
- uPwd.equals(loginPwd)){//区分大小写判断
- System.out.println("登录成功!");
- break;
- }else{
- maxCount--;
- if(maxCount == 0){
- System.out.println("对不起,三次登录失败!账户锁定两个小时!");
- break;
- }
- System.out.println("用户名或密码错误,您还有 " + maxCount + " 次机会!");
- }
- }
- }
- }
1,添加
1.1 StringBuffer append(int x); //在缓冲区的末尾追加
1.2 StringBuffer insert(int index,Stringstr); //在指定索引位置添加
2,删除
2.1 StringBuffer delete(int start, intend); //包含头索引,不包含尾部索引
2.2 StringBuffer delete(0,sb.length); //清空缓冲区
sb = new StringBuffer();
sb.append("aaaaa");
sb = new StringBuffer();
2.3 StringBuffer deleteCharAt(int index); //根据指定的索引删除索引对应的元素
3,修改
3.1 StringBuffer replace(int start,int end,string);//用String替换,包含头不包含尾
3.2 void setCharAt(int index ,char); //修改,把指定索引位置的值改成传入的char值
3.3 StringBuffer reverse(); //将缓冲区的元素反转
3.4 void setLength(int len); //根据传入的len值截取缓冲区的长度
3.5 toString() //转换成String
4,查找
4.1 int indexOf(str); //查找str在缓冲区第一次出现的位置
4.2 int lastIndexOf(str); //从后向前查找查找str在缓冲区第一次出现的位置
StringBuilder和StringBuffer
1.StringBuilder和StringBuffer与String的区别
StringBuilder和StringBuffeer是可变字符序列
String是不变得,一但被初始化,就不能改变
2.StringBuilder和StringBuffer的区别
StringBuilder是线程不安全的,所以效率比较高,1.5版本出现
StringBuffer是线程安全的,效率相对较低,1.0版本出现的
StringBuffer:
String append():向当前的StringBuffer的字符末尾追加任何类型数据,有各种重载的方法。如果当前容量不够,将增加新容量为之前容量的2倍+2
public StringBuffer insert(int offset,Stringstr):将字符串插入此字符序列中。
public StringBuffer delete(int start,intend):移除此序列的子字符串中的字符。该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,如果不存在这种字符,则一直到序列尾部。如果 start 等于 end,则不发生任何更改。
1.如果end的值超出"字符长度",不抛异常,截止到字符串末尾;
2.start不能超出"字符长度",否则:运行时抛出:java.lang.StringIndexOutOfBoundsException
3.如果start大于end,运行时抛出:java.lang.StringIndexOutOfBoundsException
public StringBuffer deleteCharAt(int index):移除此序列指定位置的 char。此序列将缩短一个 char
(1)index一定要小于"字符长度length()",否则运行时异常:java.lang.StringIndexOutOfBoundsException
public StringBuffer replace(int start,intend,String str):
使用给定 String 中的字符替换此序列的子字符串中的字符。该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,如果不存在这种字符,则一直到序列尾部。先将子字符串中的字符移除,然后将指定的 String 插入 start
public String substring(int start):
返回一个新的 String,它包含此字符序列当前所包含的字符子序列。该子字符串始于指定索引处的字符,一直到此字符串末尾。
1.start<=length()(字符长度);
public String substring(int start,int end):
(1)包含start,不包含end
(2)start > end :java.lang.StringIndexOutOfBoundsException
(3)start=end :空字符
(4)如果start或 end >= length():java.lang.StringIndexOutOfBoundsException:
public StringBuffer reverse():将此字符序列用其反转形式取代
- public class Test {
- public static void main(String[] args) {
- StringBuffer buf1 = new StringBuffer();
- buf1.append("hello");
- System.out.println("容量:" + buf1.capacity());
- System.out.println("长度:" + buf1.length());
- buf1.append(true);
- System.out.println("容量:" + buf1.capacity());
- System.out.println("长度:" + buf1.length());
- buf1.append(3.14159);
- System.out.println("容量:" + buf1.capacity());
- System.out.println("长度:" + buf1.length());
- buf1.append("121212121212121212122222");
- System.out.println("--容量:" + buf1.capacity());//如果之前容量不够,将增加原长度的2倍 + 2
- System.out.println("--长度:" + buf1.length());
- buf1.append("1");
- System.out.println("容量:" + buf1.capacity());//如果之前容量不够,将增加原长度的2倍 + 2
- System.out.println("长度:" + buf1.length());
- System.out.println("----insert()----");
- StringBuffer buf2 = new StringBuffer("Hello");
- System.out.println(buf2.insert(5, "xxx"));//offset后移,将字符串插入:Hxxxello
- // System.out.println(buf2.indexOf(6,"xxx"));//java.lang.StringIndexOutOfBoundsException(运行时)offset值一定<=字符长度
- System.out.println("----delete()----");
- StringBuffer buf3 = new StringBuffer("HelloWorld");
- System.out.println(buf3.delete(1, 2));//World
- System.out.println("----deleteCharAt()----");
- StringBuffer buf4 = new StringBuffer("HelloWorld");
- System.out.println(buf4.deleteCharAt(9));//
- System.out.println("----replace()----");
- StringBuffer buf5 = new StringBuffer("HelloWorld");
- System.out.println(buf5.replace(2, 5, "XXXX"));
- System.out.println("----substring()----");
- StringBuffer buf6 = new StringBuffer("HelloWorld");
- System.out.println(buf6.substring(10));
- System.out.println(buf6);
- System.out.println("--" + buf6.substring(7,8));
- System.out.println("----reverse()----");
- StringBuffer buf7 = new StringBuffer("你好中国");
- System.out.println(buf7.reverse());
- }
- }
1.什么是包装类
8种基本数据类型都会对应一个包装类
int是Integer, char是Character, 其他都是首字母大写double Double short Short boolean Boolean
2.什么时候使用
集合的泛型中只能写包装类型
后面的课程中会学到集合, 集合是只能装对象的, 而基本数据类型不是对象不能直接装入
在JDK5之前, 如果想把基本数据类型装入集合, 必须人工的进行包装(转为包装类对象)
JDK5之后, 基本数据类型和包装类之间可以自动的互相转换了
Integeri = 10;
- public class Test {
- public static void main(String[] args) {
- Integer intObj = 20;//相当于:Integer intObj = new Integer(20);//自动装箱
- int intValue = new Integer(20);//自动拆箱
- int sum = intObj + intValue;
- System.out.println("sum = " + sum);
- }
- }
-
BigInteger
1.创建对象
可以使用BigInteger(String)来创建一个很大的整数, 精度可以无限大, 值创建之后不会被改变(类似String)
2.常用方法
BigInteger add(BigInteger val) //加
BigInteger subtract(BigInteger val) //减
BigInteger multiply(BigInteger val) //乘
BigInteger divide(BigInteger val) //除
BigInteger mod(BigInteger m ) //模
BigInteger max(BigInteger val) //两个数的最大值
BigInteger min(BigInteger val) //两个数的最小值
1.创建对象
BigDecimal(double); //不建议用,运算结果不精确
BigDecimal(String); //可以,但是每次都要传字符串给构造函数
static BigDecimal valueOf(double) //可以,而且可以直接传double数
因为double数是不精确,是无限接近那个数,用BigDemal这个类可以让其精确
2.常用方法
BigDecimal add(BigDecimal augend)
BigDecimal subtract(BigDecimalsubtrahend)
BigDecimal multiply(BigDecimalmultiplicand)
BigDecimal divide(BigDecimal divisor)
1.Date
比较古老的一个类, 大多数方法已过时, 但通常我们还会用它来获取当前时间
new Date()可以创建日期对象, 然后使用SimpleDateFormat可以将其格式化成我们需要的格式
通常使用的格式为: "yyyy-MM-dd HH:mm:ss", 具体格式说明详见SimpleDateFormat类yyyy年MM月dd日 E HH:mm:ss
a.获取当前时间的毫秒值
Date d = new Date();
d.getTime(); //获取的是1970年1月1日0时0分0秒到当前时间的毫秒值
System.currentTimeMillis();
b.将毫秒值转换成时间对象
Date d = new Date(毫秒值) //通过毫秒值获取时间对象
Date d = new Date(); //创建时间对象
d.setTime(毫秒值); //根据毫秒值修改时间对象
2.Calendar
很多方法都是替代了Date类的方法, 最常用的就是
int get(int field)(Calendar.YEAR) //通过传入的字段获取对应的值,(获取年对应的值)
void add(int field, int amount) //field代表传入的时间字段可以是年月日等,amount代表是数值,正数就是在传入的字段上加,负数减
void set(int field, int value) //field代表传入的时间字段可以是年月日等,value代表设置的值,想设置哪一年或月日等,就写哪个值
void set(int year, int month, int date)
可以对指定的字段获取, 设置, 以及增减
提供了一些和数学运算相关的方法,
static double PI //获取π(派)的值
static double floor(double a) //是小于等于a这个double值的最大整数对应的double值
static double ceil(double a) //是大于等于a这个double值的最小整数对应的double值
static long round(double a ) //四舍五入,返回是一个long值
static double sqrt(double a) //开平方
static double pow(double a, double b) //a是底数,b是指数返回的是a的b次幂
1.什么是正则表达式
是一种字符串的约束格式, 例如在某些网站上填写邮箱的时候, 如果乱写会提示输入不合法, 这种验证就是使用正则表达式做的.
2.匹配
String里的matches() 验证一个字符串是否匹配指定的正则表达式"18612345678".matches("1[34578]\\d{9}");
3.分割
String里的split() 用指定正则表达式能匹配的字符作为分隔符, 分割字符串
4.替换
String里的replaceAll("","") 把字符串中能匹配正则表达式的部分替换为另一个字符串
5.查找
Pattern.compile() 创建正则表达式对象
Pattern.matcher() 用正则表达式匹配一个字符串, 得到匹配器
Matcher.find() 查找字符串中是否包含能匹配正则表达式的部分
Matcher.group() 获取匹配的部分
- public class Test {
- public static void main(String[] args) {
- String str = "hid";
- /*********正则表达式-字符类************/
- //字符串中是否以h开头,以d结尾,而且中间只有一个字符,而且是元音字母a、e、i、o、u?
- String regex = "h[aeiou]d";
- System.out.println(str.matches(regex));
- //字符串中是否以h开头,以d结尾,而且中间只有一个小写英文字母?
- regex = "h[a-z]d";
- System.out.println(str.matches(regex));
- //字符串是否以大写英文字母开头,后跟id?
- String s = "A";
- regex = "[A-Z]id";//"[" + s + "-Z]id"
- System.out.println(str.matches(regex));
- //字符串首字母是否非数字?
- regex = "[^0-9]id";
- System.out.println(str.matches(regex));
- /********正则表达式-逻辑运算符**********/
- //判断小写辅音字母:
- String s1 = "ba";
- regex = "[a-z&&[^aeiou]]a";
- //判断首字母为h 或 H ,后跟一个元音字母,并以 d 结尾
- regex = "[hH][aeiou]d";
- /*******正则表达式-预定义字符类********/
- regex = "[0-9][0-9][0-9]";//使用字符类的方式
- regex = "\\d\\d\\d";//使用预定义字符类的方式:\\d 相当于[0-9]
- s1 = "23";
- System.out.println(s1.matches(regex));
- //2.判断字符串是否以h开头,中间是任何字符,并以d结尾:"h.d"
- regex = "h[.]d";//只匹配:h.d
- regex = "h.d";//匹配:h[任何字符]d
- s1 = "h中d";
- System.out.println(s1.matches(regex));
- //3.判断字符串是否是”had.”:
- regex = "had\\.";//可以
- regex = "had[.]";//也可以
- s1 = "hadd";
- System.out.println(s1.matches(regex));
- //4.判断手机号码(1开头,第二位是:3,4,5,7,8,后跟9位数字):
- regex = "1[34578][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]";
- regex = "1[34578]\\d\\d\\d\\d\\d\\d\\d\\d\\d";
- s1 = "19513865765";
- System.out.println(s1.matches(regex));
- /******使用限定符***********/
- regex = "1[34578]\\d{9}";//相当于:1[34578][0-9]{9}
- //1.判断1位或多位数字
- regex = "\\d+";
- s1 = "1";
- System.out.println(s1.matches(regex));
- //2.判断小数(小数点最多1次):
- regex = "\\d+\\.?\\d+";
- s1 = "3.1415";
- System.out.println(s1.matches(regex));
- //3.判断数字(可出现或不出现小数部分):
- regex = "\\d+(\\.?\\d+)?";
- s1 = "22.";
- System.out.println(s1.matches(regex));
- //4.满足:"22."的格式
- regex = "\\d+(\\.?\\d*)?";
- s1 = "22.";
- System.out.println(s1.matches(regex));
- //5.满足:+20,-3,22.格式:
- regex = "[+-]?\\d+(\\.?\\d*)?";
- s1 = "+-22.3";
- System.out.println(s1.matches(regex));
- }
- }
位置:java.lang.Object
它是所有类的超类(包括数组,自定义类)
一些方法:
public String toString();
public boolean equals(Object obj);
protected void finalize():用于垃圾回收;
public final Class<?> getClass();
public int hashCode();
toString()方法用于打印对象信息:
Student stu = new Student();
System.out.println(stu);
- public class Student{
- String name;
- int age;
- @Override
- public String toString(){
- return “Student [name=“ + name +
- “,age=“ + age + “]”);
- public static void main(){
- Student stu = new Student();
- System.out.println(stu);
- }
System类提供的设施中,有标准输入、标准输出和错误输出流;
对外部定义的属性和环境变量的访问;加载文件和库的方法;
还有快速复制数组的一部分的实用方法。
System类要掌握的功能
exit(int state)
currentTimeMillis();
getPropertie(Stringkey);
arraycopy ()
一、String类:
1.String 类代表字符串。Java 程序中的所有字符串字面值(如"abc" )都作为此类的实例实现。
2.字符串是常量;它们的值在创建之后不能更改。
3.String 对象是不可变的,所以可以共享。
一.String类的构造方法:
1).public String():默认构造方法,没有任何参数
2).参数为字节数组:
String(byte[]bytes);
String(byte[] bytes,int startIndex,intlength)
3).参数为字符数组:
String(char[]value);
String(char[] value,intstartIndex,int length)
4).参数为字符串
String(String str):
1.判断定义为String类型的s1和s2是否相等
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
2.下面这句话在内存中创建了几个对象?
String s1 = new String("abc"); 2次 一次在堆内存中new开辟了个空间 另一次在方法区创建了"abc"
3.判断定义为String类型的s1和s2是否相等
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2); 不等于 这个比较地址了
System.out.println(s1.equals(s2)); 等于 这个是比较内容了
4.判断定义为String类型的s1和s2是否相等
String s1 = "a" + "b"+ "c";
String s2 = "abc";
System.out.println(s1 == s2); 等于 俩的地址相同
System.out.println(s1.equals(s2)); 等于 内容也相同
5.判断定义为String类型的s1和s2是否相等
String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2); 不等于 因为地址不一样
System.out.println(s3.equals(s2)); 等于 内容相同
1,判断
1.1boolean equals(Object); //判断传入的字符串是否与调用的字符串字符序列是否相同,相同就返回true否则false
1.2 boolean equalsIgnoreCase(string); //判断传入的字符串是否与调用的字符串字符序列是否相同,不区分大小写,相同就返回true否则false
1.3 boolean contains(string); //判断传入的字符串是否被调用的字符串包含
1.4 boolean startsWith(string); //判断调用的字符串是否以传入的字符串开头
1.5 boolean endsWith(string); //判断调用的字符串是否以传入的字符串结尾
1.6 boolean isEmpty(); //判断字符串是否为空
2,获取
2.1 int length(); //获取字符串的长度
2.2 char charAt(index); //通过索引获取对应的字符
2.3 int indexOf(int ch); //通过传入int数或者是字符找对应索引
int idnexOf(int ch,fromIndex); //在指定fromIndex的位置查找传入的字符
2.4 int indexOf(string str); //通过传入字符串查找字符串所对应的索引
int idnexOf(string str,fromIndex); //通过指定fromIndex的位置查找传入的字符串
2.5 int lastIndexOf(ch); //通过传入的字符从后向前找字符的索引值,把从后向前第一次找到的索引值返回
int lastIndexOf(ch,fromIndex): //通过指定fromIndex的位置,从后向前查找字符,把从后向前第一次找到的索引值返回
2.6 int lastIndexOf(string); //通过传入的字符串,从后向前查找,将第一次找到字符串中第一个字符的索引返回
int lastIndexOf(string,fromIndex): //通过指定fromIndex的位置,从后向前查找对应字符串,将第一次找到字符串中第一个字符的索引返回
2.7 String substring(start); //通过传入的索引值开始向后截取,截取的是索引到length
String substring(start,end); //通过传入的两个索引值截取,有开始有结尾,包含头不包含尾
3,转换
3.1 byte[] getBytes(); //编码,让计算机看的懂的,用默认的编码表,将字符串转换成字节数组
byte[] getBytes(String) //用指定的编码表进行编码
3.2 char[] toCharArray(); //将字符串转换成字符数组
3.3 static String copyValueOf(char[]); //将字符数组转换成字符串
static String copyValueOf(char[] data, int offset, int count);//将字符数组转换字符串,通过offset开始,截取count个
3.4 static String valueOf(char[]); //将字符数组转换成字符串
static String valueOf(char[] data, int offset, int count);//将字符数组转换字符串,通过offset开始,截取count个
3.5 static String valueOf(int); //将一个int数转换成字符串
static String valueOf(double); //同上 是double
static String valueOf(boolean); //同上 是boolean
...
3.6 static String valueOf(object);
和object.toString():结果是一样的。
3.7 String toLowerCase(): //将字符串全部转换为小写
String toUpperCase(): //将字符串全班转换为大写
3.8"abc".concat("kk"); //将两个字符串相连接,产生新的字符串
4,替换。
4.1 String replace(oldChar,newChar); //将newChar替换OldChar,如果OldChar不存在,原字符串直接赋值给替换后字符串
4.2 String replace(string,string);
5,切割。
String[] split(regex); //通过regex切割字符串,切割后会产生一个字符串数组
String s = "金三胖 郭美美 李天一";
String[] arr = s.split(" ");
6,去除字符串两空格。
String trim();
7,比较
String str = "ab";
String str1 = "bc";
int num = str.compareTo(str1); //如果str比str1大的话,返回的正数
byte[] arr = {97,98,99};
String str = new String(byte[]) //解码,让我们看的懂的,通过默认的编码表,将字节数组转换成字符串
String(byte[], String) //解码,通过指定的编码表,将字节数组转换成字符串
String(byte[],int offset, int length, String)//解码,截取字节数组,offset是开始索引,length是截取的长度
1.创建对象
使用构造函数Scanner(InputStream)传入一个输入流, 该Scanner就可以读取数据了System.in
2.读取各种类型的数据
nextInt() 可以读取一个int
nextLine() 读取一行字符串
3.关闭问题
使用结束后要调用close()方法释放资源
- public class Test1 {
- public static void main(String[] args) {
- String str1 = new String();
- System.out.println("String str1 = new String():");
- System.out.println("str1 == null : " + (str1 == null));//str1是空引用么? //false
- System.out.println("str1.length():" + str1.length());//str1中存储的字符串的长度? //0
- System.out.println("str1.equals(\"\"):" + str1.equals(""));//str1中是空字符串么?//true
- System.out.println("---------------------");
- /*
- * 参数为字节数组:
- String(byte[] bytes);
- String(byte[] bytes,int startIndex,int length)
- */
- byte[] bArray = {97,98,99,100};
- String str2 = new String(bArray);//使用byte数组构造一个String,如果byte数组都是整数,对应0-127就是ASCII码表的值
- System.out.println("str2 = " + str2);
- String str3 = new String(bArray,1,3);//用bArray来构造,从bArray的索引1开始,取2个
- // String str3 = new String(bArray,1,4);// java.lang.StringIndexOutOfBoundsException:(运行时异常)
- System.out.println("str3 = " + str3);
- /*
- * 参数为字符数组:
- String(char[] value);
- String(char[] value,int startIndex,int length)
- */
- char[] cArray = {'[','a','b',',','c','d',']'};
- String str4 = new String(cArray);
- System.out.println("str4 = " + str4);
- String str5 = new String(cArray,1,5);
- System.out.println("str5 = " + str5);
- /*
- * 用一个字符串构造一个字符串:
- * String(String str):
- */
- String str6 = new String("abc");
- System.out.println("str6 = " + str6);
- }
- }
- public class Test {
- public static void main(String[] args) {
- //在下面的过程中,产生了几个String对象空间
- //这个过程中,先后产生了三个对象空间:"hello","world","helloworld"
- //这个过程中,更改是str1的引用,
- String str1 = "hello";
- str1 = str1 + "world";
- System.out.println(str1);
- System.out.println("---------------------");
- //字符串的字面量
- String str2 = "ab";//先在常量池中找有没有"ab",如果没有,建立空间,返回引用;
- String str3 = "ab";//先在常量池中找有没有"ab",如果有,直接返回"ab"的引用;
- System.out.println(str2 == str3);//判断两个引用是否相等:true
- System.out.println(str2.equals(str3));//判断里面存储的字符串是否相同:true
- System.out.println("---------------------");
- String str4 = "a";
- String str5 = "b";
- String str6 = "ab";
- String str7 = str4 + str5;//"ab"//运算符的操作数有一个是变量,结果就是一个新字符串;
- //str7 = "a" + str5;//运算符的操作数有一个是变量,结果就是一个新字符串;
- //str7 = str4 + "b";//运算符的操作数有一个是变量,结果就是一个新字符串;
- String str8 = "a" + "b";//运算符的操作数都是字符串常量,这个结果可以确定,会先在常量池中找
- System.out.println("str6 == str7 : " + (str6 == str7));//false
- System.out.println("str6 == str8 : " + (str6 == str8));//true
- System.out.println("---------------------");
- //字符串的new 操作符
- String str9 = new String("ab");
- String str10 = new String("ab");
- System.out.println("str9 == str10 : " + str9 == str10);//肯定不等false
- System.out.println("str9.equals(str10) : " + str9.equals(str10));//肯定是true
- String str11 = "ab";
- System.out.println("str9 == str11 : " + str9 == str11);//false
- }
- }
- public class Test {
- public static void main(String[] args) {
- //假设用户已有的登录名和密码:admin 和 admin
- String loginName = "admin";
- String loginPwd = "admin";
- Scanner sc = new Scanner(System.in);
- int maxCount = 3;//最多3次机会
- while(maxCount > 0){
- System.out.print("请输入登录名:");
- String uName = sc.next();
- System.out.print("请输入登录密码:");
- String uPwd = sc.next();
- if(uName.equals(loginName) &&
- uPwd.equals(loginPwd)){//区分大小写判断
- System.out.println("登录成功!");
- break;
- }else{
- maxCount--;
- if(maxCount == 0){
- System.out.println("对不起,三次登录失败!账户锁定两个小时!");
- break;
- }
- System.out.println("用户名或密码错误,您还有 " + maxCount + " 次机会!");
- }
- }
- }
- }
1,添加
1.1 StringBuffer append(int x); //在缓冲区的末尾追加
1.2 StringBuffer insert(int index,Stringstr); //在指定索引位置添加
2,删除
2.1 StringBuffer delete(int start, intend); //包含头索引,不包含尾部索引
2.2 StringBuffer delete(0,sb.length); //清空缓冲区
sb = new StringBuffer();
sb.append("aaaaa");
sb = new StringBuffer();
2.3 StringBuffer deleteCharAt(int index); //根据指定的索引删除索引对应的元素
3,修改
3.1 StringBuffer replace(int start,int end,string);//用String替换,包含头不包含尾
3.2 void setCharAt(int index ,char); //修改,把指定索引位置的值改成传入的char值
3.3 StringBuffer reverse(); //将缓冲区的元素反转
3.4 void setLength(int len); //根据传入的len值截取缓冲区的长度
3.5 toString() //转换成String
4,查找
4.1 int indexOf(str); //查找str在缓冲区第一次出现的位置
4.2 int lastIndexOf(str); //从后向前查找查找str在缓冲区第一次出现的位置
StringBuilder和StringBuffer
1.StringBuilder和StringBuffer与String的区别
StringBuilder和StringBuffeer是可变字符序列
String是不变得,一但被初始化,就不能改变
2.StringBuilder和StringBuffer的区别
StringBuilder是线程不安全的,所以效率比较高,1.5版本出现
StringBuffer是线程安全的,效率相对较低,1.0版本出现的
StringBuffer:
String append():向当前的StringBuffer的字符末尾追加任何类型数据,有各种重载的方法。如果当前容量不够,将增加新容量为之前容量的2倍+2
public StringBuffer insert(int offset,Stringstr):将字符串插入此字符序列中。
public StringBuffer delete(int start,intend):移除此序列的子字符串中的字符。该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,如果不存在这种字符,则一直到序列尾部。如果 start 等于 end,则不发生任何更改。
1.如果end的值超出"字符长度",不抛异常,截止到字符串末尾;
2.start不能超出"字符长度",否则:运行时抛出:java.lang.StringIndexOutOfBoundsException
3.如果start大于end,运行时抛出:java.lang.StringIndexOutOfBoundsException
public StringBuffer deleteCharAt(int index):移除此序列指定位置的 char。此序列将缩短一个 char
(1)index一定要小于"字符长度length()",否则运行时异常:java.lang.StringIndexOutOfBoundsException
public StringBuffer replace(int start,intend,String str):
使用给定 String 中的字符替换此序列的子字符串中的字符。该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,如果不存在这种字符,则一直到序列尾部。先将子字符串中的字符移除,然后将指定的 String 插入 start
public String substring(int start):
返回一个新的 String,它包含此字符序列当前所包含的字符子序列。该子字符串始于指定索引处的字符,一直到此字符串末尾。
1.start<=length()(字符长度);
public String substring(int start,int end):
(1)包含start,不包含end
(2)start > end :java.lang.StringIndexOutOfBoundsException
(3)start=end :空字符
(4)如果start或 end >= length():java.lang.StringIndexOutOfBoundsException:
public StringBuffer reverse():将此字符序列用其反转形式取代
- public class Test {
- public static void main(String[] args) {
- StringBuffer buf1 = new StringBuffer();
- buf1.append("hello");
- System.out.println("容量:" + buf1.capacity());
- System.out.println("长度:" + buf1.length());
- buf1.append(true);
- System.out.println("容量:" + buf1.capacity());
- System.out.println("长度:" + buf1.length());
- buf1.append(3.14159);
- System.out.println("容量:" + buf1.capacity());
- System.out.println("长度:" + buf1.length());
- buf1.append("121212121212121212122222");
- System.out.println("--容量:" + buf1.capacity());//如果之前容量不够,将增加原长度的2倍 + 2
- System.out.println("--长度:" + buf1.length());
- buf1.append("1");
- System.out.println("容量:" + buf1.capacity());//如果之前容量不够,将增加原长度的2倍 + 2
- System.out.println("长度:" + buf1.length());
- System.out.println("----insert()----");
- StringBuffer buf2 = new StringBuffer("Hello");
- System.out.println(buf2.insert(5, "xxx"));//offset后移,将字符串插入:Hxxxello
- // System.out.println(buf2.indexOf(6,"xxx"));//java.lang.StringIndexOutOfBoundsException(运行时)offset值一定<=字符长度
- System.out.println("----delete()----");
- StringBuffer buf3 = new StringBuffer("HelloWorld");
- System.out.println(buf3.delete(1, 2));//World
- System.out.println("----deleteCharAt()----");
- StringBuffer buf4 = new StringBuffer("HelloWorld");
- System.out.println(buf4.deleteCharAt(9));//
- System.out.println("----replace()----");
- StringBuffer buf5 = new StringBuffer("HelloWorld");
- System.out.println(buf5.replace(2, 5, "XXXX"));
- System.out.println("----substring()----");
- StringBuffer buf6 = new StringBuffer("HelloWorld");
- System.out.println(buf6.substring(10));
- System.out.println(buf6);
- System.out.println("--" + buf6.substring(7,8));
- System.out.println("----reverse()----");
- StringBuffer buf7 = new StringBuffer("你好中国");
- System.out.println(buf7.reverse());
- }
- }
1.什么是包装类
8种基本数据类型都会对应一个包装类
int是Integer, char是Character, 其他都是首字母大写double Double short Short boolean Boolean
2.什么时候使用
集合的泛型中只能写包装类型
后面的课程中会学到集合, 集合是只能装对象的, 而基本数据类型不是对象不能直接装入
在JDK5之前, 如果想把基本数据类型装入集合, 必须人工的进行包装(转为包装类对象)
JDK5之后, 基本数据类型和包装类之间可以自动的互相转换了
Integeri = 10;
- public class Test {
- public static void main(String[] args) {
- Integer intObj = 20;//相当于:Integer intObj = new Integer(20);//自动装箱
- int intValue = new Integer(20);//自动拆箱
- int sum = intObj + intValue;
- System.out.println("sum = " + sum);
- }
- }
-
BigInteger
1.创建对象
可以使用BigInteger(String)来创建一个很大的整数, 精度可以无限大, 值创建之后不会被改变(类似String)
2.常用方法
BigInteger add(BigInteger val) //加
BigInteger subtract(BigInteger val) //减
BigInteger multiply(BigInteger val) //乘
BigInteger divide(BigInteger val) //除
BigInteger mod(BigInteger m ) //模
BigInteger max(BigInteger val) //两个数的最大值
BigInteger min(BigInteger val) //两个数的最小值
1.创建对象
BigDecimal(double); //不建议用,运算结果不精确
BigDecimal(String); //可以,但是每次都要传字符串给构造函数
static BigDecimal valueOf(double) //可以,而且可以直接传double数
因为double数是不精确,是无限接近那个数,用BigDemal这个类可以让其精确
2.常用方法
BigDecimal add(BigDecimal augend)
BigDecimal subtract(BigDecimalsubtrahend)
BigDecimal multiply(BigDecimalmultiplicand)
BigDecimal divide(BigDecimal divisor)
1.Date
比较古老的一个类, 大多数方法已过时, 但通常我们还会用它来获取当前时间
new Date()可以创建日期对象, 然后使用SimpleDateFormat可以将其格式化成我们需要的格式
通常使用的格式为: "yyyy-MM-dd HH:mm:ss", 具体格式说明详见SimpleDateFormat类yyyy年MM月dd日 E HH:mm:ss
a.获取当前时间的毫秒值
Date d = new Date();
d.getTime(); //获取的是1970年1月1日0时0分0秒到当前时间的毫秒值
System.currentTimeMillis();
b.将毫秒值转换成时间对象
Date d = new Date(毫秒值) //通过毫秒值获取时间对象
Date d = new Date(); //创建时间对象
d.setTime(毫秒值); //根据毫秒值修改时间对象
2.Calendar
很多方法都是替代了Date类的方法, 最常用的就是
int get(int field)(Calendar.YEAR) //通过传入的字段获取对应的值,(获取年对应的值)
void add(int field, int amount) //field代表传入的时间字段可以是年月日等,amount代表是数值,正数就是在传入的字段上加,负数减
void set(int field, int value) //field代表传入的时间字段可以是年月日等,value代表设置的值,想设置哪一年或月日等,就写哪个值
void set(int year, int month, int date)
可以对指定的字段获取, 设置, 以及增减
提供了一些和数学运算相关的方法,
static double PI //获取π(派)的值
static double floor(double a) //是小于等于a这个double值的最大整数对应的double值
static double ceil(double a) //是大于等于a这个double值的最小整数对应的double值
static long round(double a ) //四舍五入,返回是一个long值
static double sqrt(double a) //开平方
static double pow(double a, double b) //a是底数,b是指数返回的是a的b次幂
1.什么是正则表达式
是一种字符串的约束格式, 例如在某些网站上填写邮箱的时候, 如果乱写会提示输入不合法, 这种验证就是使用正则表达式做的.
2.匹配
String里的matches() 验证一个字符串是否匹配指定的正则表达式"18612345678".matches("1[34578]\\d{9}");
3.分割
String里的split() 用指定正则表达式能匹配的字符作为分隔符, 分割字符串
4.替换
String里的replaceAll("","") 把字符串中能匹配正则表达式的部分替换为另一个字符串
5.查找
Pattern.compile() 创建正则表达式对象
Pattern.matcher() 用正则表达式匹配一个字符串, 得到匹配器
Matcher.find() 查找字符串中是否包含能匹配正则表达式的部分
Matcher.group() 获取匹配的部分
- public class Test {
- public static void main(String[] args) {
- String str = "hid";
- /*********正则表达式-字符类************/
- //字符串中是否以h开头,以d结尾,而且中间只有一个字符,而且是元音字母a、e、i、o、u?
- String regex = "h[aeiou]d";
- System.out.println(str.matches(regex));
- //字符串中是否以h开头,以d结尾,而且中间只有一个小写英文字母?
- regex = "h[a-z]d";
- System.out.println(str.matches(regex));
- //字符串是否以大写英文字母开头,后跟id?
- String s = "A";
- regex = "[A-Z]id";//"[" + s + "-Z]id"
- System.out.println(str.matches(regex));
- //字符串首字母是否非数字?
- regex = "[^0-9]id";
- System.out.println(str.matches(regex));
- /********正则表达式-逻辑运算符**********/
- //判断小写辅音字母:
- String s1 = "ba";
- regex = "[a-z&&[^aeiou]]a";
- //判断首字母为h 或 H ,后跟一个元音字母,并以 d 结尾
- regex = "[hH][aeiou]d";
- /*******正则表达式-预定义字符类********/
- regex = "[0-9][0-9][0-9]";//使用字符类的方式
- regex = "\\d\\d\\d";//使用预定义字符类的方式:\\d 相当于[0-9]
- s1 = "23";
- System.out.println(s1.matches(regex));
- //2.判断字符串是否以h开头,中间是任何字符,并以d结尾:"h.d"
- regex = "h[.]d";//只匹配:h.d
- regex = "h.d";//匹配:h[任何字符]d
- s1 = "h中d";
- System.out.println(s1.matches(regex));
- //3.判断字符串是否是”had.”:
- regex = "had\\.";//可以
- regex = "had[.]";//也可以
- s1 = "hadd";
- System.out.println(s1.matches(regex));
- //4.判断手机号码(1开头,第二位是:3,4,5,7,8,后跟9位数字):
- regex = "1[34578][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]";
- regex = "1[34578]\\d\\d\\d\\d\\d\\d\\d\\d\\d";
- s1 = "19513865765";
- System.out.println(s1.matches(regex));
- /******使用限定符***********/
- regex = "1[34578]\\d{9}";//相当于:1[34578][0-9]{9}
- //1.判断1位或多位数字
- regex = "\\d+";
- s1 = "1";
- System.out.println(s1.matches(regex));
- //2.判断小数(小数点最多1次):
- regex = "\\d+\\.?\\d+";
- s1 = "3.1415";
- System.out.println(s1.matches(regex));
- //3.判断数字(可出现或不出现小数部分):
- regex = "\\d+(\\.?\\d+)?";
- s1 = "22.";
- System.out.println(s1.matches(regex));
- //4.满足:"22."的格式
- regex = "\\d+(\\.?\\d*)?";
- s1 = "22.";
- System.out.println(s1.matches(regex));
- //5.满足:+20,-3,22.格式:
- regex = "[+-]?\\d+(\\.?\\d*)?";
- s1 = "+-22.3";
- System.out.println(s1.matches(regex));
- }
- }
位置:java.lang.Object
它是所有类的超类(包括数组,自定义类)
一些方法:
public String toString();
public boolean equals(Object obj);
protected void finalize():用于垃圾回收;
public final Class<?> getClass();
public int hashCode();
toString()方法用于打印对象信息:
Student stu = new Student();
System.out.println(stu);
- public class Student{
- String name;
- int age;
- @Override
- public String toString(){
- return “Student [name=“ + name +
- “,age=“ + age + “]”);
- public static void main(){
- Student stu = new Student();
- System.out.println(stu);
- }
System类提供的设施中,有标准输入、标准输出和错误输出流;
对外部定义的属性和环境变量的访问;加载文件和库的方法;
还有快速复制数组的一部分的实用方法。
System类要掌握的功能
exit(int state)
currentTimeMillis();
getPropertie(Stringkey);
arraycopy ()