工具类

工具类

String类

String类可以存储字符序列,它不是基本数据类型,而是引用类型。 创建字符串对象的两种方式:

package day16;
public class Day1604 {
public static void main(String[] args) {
//String是引用类型
//str1引用了一个字符串对象
String str1 = "hello";//使用字面值创建字符串对象
String str2 = new String("hello");//使用构造器创建字符串对象
}
}

String类内部使用字符数组来保存字符串。

public class Day1605 {
public static void main(String[] args) {
char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
//通过字符数组来创建一个字符串对象
String str1 = new String(chars);
System.out.println(str1);//abcdefg
//将字符串对象转换为一个字符串数组
char[] arr = str1.toCharArray();
for (char c : arr) {
System.out.print(c);
} S
ystem.out.println();
}
}

字符串对象是不可变的,如果一个对象没有实例方法可以修改自身的属性,那么这个对象就是不可变的。
对字符串对象的任何修改都将创建一个新的对象,而原来的字符串对象保存不变。

package day16;
    public class Day1606 {
    public static void main(String[] args) {
    String str1 = "value1";
    String str3 = str1;
    //concat方法将参数连接到此字符串的末尾
    String str2 = str1.concat("value2");
    System.out.println(str1);//value1,不改变原来字符串对象
    System.out.println(str2);//value1value2
    str1 = "value3";//str1引用另外一个对象,原来的对象仍然没有变
    System.out.println(str1);//value3
    System.out.println(str3);//value1
    }
    }

所有的字符串字面值都被存储在字符串常量池中。如果编译器发现了一个字符串字面值,它首先会检查字符串常量池中有没有,如果有的话直接重用,如果没有,它会创建一个新的字符串对象,并放入常量池中。

package day16;
public class Day1607 {
public static void main(String[] args) {
String str1 = "value"; //创建一个字符串对象,放入字符串常量池,并将其引用赋值给str1
String str2 = "value";//从字符串常量池中获取对象,并将引用赋值给str2
System.out.println(str1 == str2); //true,str1和str2引用同一个字符串对象
}
}

package day16;
public class Day1608 {
public static void main(String[] args) {
String str1 = "value"; //创建一个字符串对象,放入字符串常量池,并将其引用赋值给str1
String str2 = new String("value");//使用new操作符总是创建对象
System.out.println(str1 == str2); //false
}
}

package day16;
public class Day1609 {
public static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(str1 == str2);//false
//String类重写了Object类的equals方法,比较的是字符串对象的内容
System.out.println(str1.equals(str2));//true
}
}

package day16;
public class Day1610 {
public static void main(String[] args) {
String str1 = new String("abc");//创建了几个对象,2
}
}

String类的常用方法

获取字符串的信息

public class Day1611 {
public static void main(String[] args) {
// char charAt(int paramInt),获取指定索引处的字符
System.out.println("abcdefg".charAt(2)); // c
// boolean contains(CharSequence s),测试此字符串是否包含指定的字符序列
System.out.println("abcdefg".contains("cde"));// true
// int length(),返回此字符串的长度。
System.out.println("ABCDEFGH".length());// 8
// boolean startsWith(String prefix),测试此字符串是否以指定的前缀开始。
System.out.println("abcdefghij".startsWith("abc")); // true
// boolean endsWith(String suffix),测试此字符串是否以指定的后缀结束。
System.out.println("abcdefghij".endsWith("ij")); // true
// int indexOf(int ch),返回指定字符在此字符串中第一次出现处的索引。
System.out.println("abcdefghij".indexOf('c'));// 2
// int indexOf(int ch, int fromIndex),返回在此字符串中第一次出现指定字符处的索引,从
指定的索引开始搜索。
System.out.println("abcdefghij".indexOf('c', 3));// -1// int indexOf(String str),返回指定子字符串在此字符串中第一次出现处的索引。
// int indexOf(String str, int fromIndex),返回指定子字符串在此字符串中第一次出现处
的索引,从指定的索引开始。
// int lastIndexOf(int ch),返回指定字符在此字符串中最后一次出现处的索引。
// int lastIndexOf(int ch, int fromIndex),返回指定字符在此字符串中最后一次出现处的
索引,从指定的索引处开始进行反向搜索。
// int lastIndexOf(String str),返回指定子字符串在此字符串中最后一次出现处的索引。
// int lastIndexOf(String str, int fromIndex),返回指定子字符串在此字符串中最后一次
出现处的索引,从指定的索引处开始进行反向搜索。
// boolean equalsIgnoreCase(String anotherString)
// 将此 字符串 与另一个 字符串 比较,不考虑大小写。
System.out.println("ABC".equalsIgnoreCase("abc"));// true
}
}

字符串操作方法 字符串对象是不可变的,对字符串对象的任何修改都将创建一个新的对象,而原来的字符串对象保存不变

package day16;
public class Day1612 {
public static void main(String[] args) {
String str = "abcdefghijk";
//String concat(String paramString),将指定字符串连接到此字符串的结尾。
System.out.println(str.concat("lmn"));//abcdefghijklmn
System.out.println(str);//abcdefghijk
//String replace(char oldChar, char newChar)
//返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
System.out.println("012301230123".replace('0', '4'));//412341234123
//String replace(CharSequence target, CharSequence replacement)
//返回一个新的字符串,它是通过用 replacement 替换此字符串中出现的所有 target 得到的。
System.out.println("012301230123".replace("01", "45"));//452345234523
//String toLowerCase()
//将此 String 中的所有字符都转换为小写。
System.out.println("ABCDEFGHIJ".toLowerCase()); //abcdefghij
//String toUpperCase()
//将此 String 中的所有字符都转换为大写。
System.out.println("abcdefghij".toUpperCase()); //ABCDEFGHIJ
//String trim()
//返回字符串的副本,忽略前导空白和尾部空白。
System.out.println(" ab cd ".trim()); //ab cd
//String substring(int beginIndex),获取从指定索引处开始的子字符串
System.out.println("abcdefghij".substring(3)); //defghij//String substring(int beginIndex,int endIndex)
//获取beginIndex和endIndex之间的子字符串,包含开始字符,不包含结束字符。
System.out.println("abcdefghij".substring(3,7)); //defg
}
}

字符串连接

对字符串使用+号代表连接,结果是字符串。
规则1:数值+数值=数值 规则2:数值+字符串=字符串
规则3:除非有括号,否则表达式的求值顺序是从左到右

package day16;
public class Day1613 {
public static void main(String[] args) {
System.out.println(5 + "Test" + 5); //5Test5
System.out.println(5 + 5 + "Test"); //10Test
System.out.println("5" + 5 + "Test"); //55Test
System.out.println("5" + "5" + "25"); //5525
System.out.println(5 + 5 + "25"); //1025
System.out.println("" + 5 + 5 + "25"); //5525
System.out.println(5 + (5 + "25")); //5525
System.out.println(5 + 5 + 25); //35
}
}

StringBuilder和StringBuffer

频繁的使用+操作符进行字符串拼接会创建大量的字符串对象,更加优雅的做法是使用StringBuilder和
StringBuffer。 StringBuilder不是线程安全的,但是执行效率高。StringBuffer是线程安全的,但是执行效率低。

package day16;
public class Day1614 {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder();
//将世追加到StringBuilder对象的末尾
s1.append("世");
//将界追加到StringBuilder对象的末尾
s1.append("界");
//将和追加到StringBuilder对象的末尾
s1.append("和");
//将平追加到StringBuilder对象的末尾
s1.append("平");
//将内容以字符串对象返回
String str = s1.toString();
System.out.println(str);//世界和平// append方法将参数拼接到内部的字符数组中并返回this,因此可以进行后续拼接
s1.append("1").append('2').append('3');
System.out.println(s1);
}
}

StringBuffer的方法和StringBuilder的方法一样

package day16;
public class Day1614 {
public static void main(String[] args) {
StringBuffer s1 = new StringBuffer();
//将世追加到StringBuilder对象的末尾
s1.append("世");
//将界追加到StringBuilder对象的末尾
s1.append("界");
//将和追加到StringBuilder对象的末尾
s1.append("和");
//将平追加到StringBuilder对象的末尾
s1.append("平");
//将内容以字符串对象返回
String str = s1.toString();
System.out.println(str);//世界和平
// append方法将参数拼接到内部的字符数组中并返回this,因此可以进行后续拼接
s1.append("1").append('2').append('3');
System.out.println(s1);
}
}

Math数学类

package day16;
public class Day1615 {
public static void main(String[] args) {
System.out.println(Math.sqrt(2.25));//平方根 1.5
System.out.println(Math.abs(-2));//绝对值 2
System.out.println(Math.pow(3, 3));//幂值 27.0
System.out.println(Math.max(3, 4));//最大值4
System.out.println(Math.min(3, 4));//最小值3
System.out.println(Math.random());//大于等于0,小于1的double类型的数
System.out.println(Math.round(14.5));//四舍五入 15
//伪随机数
//5-10之间的随机数
int i = (int) (Math.random() * 5) + 5;
System.out.println(i);}
}

Date日期类

java.util.Date类现在已经不推荐使用了,此类的很多方法都被标注为已过时。Date类对象存储从1970年1月1日0时0分0秒到创建时的毫秒数

public class Day1614 {

public static void main(String[] args) throws ParseException {
Date date = new Date();
//返回距1970年1月1日0时0分0秒的毫秒数
System.out.println(date.getTime());//1562508319386
System.out.println(date);//Sun Jul 07 22:06:35 CST 2019
//对于日期的所有操作都需要将时间转换为毫秒数来进行修改。
//增加6个小时
date.setTime(date.getTime() + 6 * 60 * 60 * 1000);
System.out.println(date);//Mon Jul 08 04:09:12 CST 2019
//减少6个小时
date.setTime(date.getTime() - 6 * 60 * 60 * 1000);
System.out.println(date);//Sun Jul 07 22:10:25 CST 2019
//可以使用SimpleDateFormat对日期进行格式化。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
String str = sdf.format(date);
System.out.println(str);//2019-07-07:22:14:16
//将一个字符串解析成一个日期对象
Date d = sdf.parse("2019-07-07:22:14:16");
System.out.println(d);//Sun Jul 07 22:14:16 CST 2019
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值