package org.forten.api.string;
import java.util.Arrays;
public class StringTest004 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("abc");
String s3 = new String("abcde");
String s4 = new String("bc");
String s5 = new String("Abc");
//str1.compareTo(str)
//按字典顺序比较str1和str2,A<Z<a<z,但此方法不会忽略大小写
System.out.println(s1.compareTo(s2));
System.out.println(s2.compareTo(s3));
System.out.println(s2.compareTo(s4));
System.out.println(s1.compareTo(s5));
//s1.compareToIgnoreCase(s2)
//是大小写不敏感的字符串比较
System.out.println(s1.compareToIgnoreCase(s5));
//s1.concat(s5)=>s1+s5
s3 = s1.concat(s5);
s4 = s1 + s5;
System.out.println(s3);
System.out.println(s4);
s1 = "def";
s2 = "abcdefgxyz";
s3 = "ab";
s4 = "yz";
//判定一个字符串是否以另一个字符串开始
System.out.println(s2.startsWith(s3));
//判定一个字符串是否以另一个字符串结束
System.out.println(s2.endsWith(s4));
//判定一个字符串是否包含另一个字符串
System.out.println(s2.contains(s1));
s1 = "abcdeabcdexyzabcd";
//使用indexof()方法可以替代以下两个参数的startWith()
System.out.println("abc".startsWith("cde", 12));
//int indexof(String str)
//在大串中查找参数str从左向右顺序,第一次出现的索引位置,如果str不存在于大串中,返回-1
//int indexOf(String str,int fromIndex)
//在大串中,使用fromIdex字符处开始查找参数str,方向是从左向右顺序,第一次出现的索引位置,如果str不存在于大串中,返回-1
//通常使用此方法查询某个子串是否存在于大串中,用是否返回-1判定
System.out.println(s1.indexOf("cd"));
System.out.println(s1.indexOf("cd", 10));
System.out.println(s1.indexOf("cdz"));
System.out.println(s1.indexOf("cdz", 10));
System.out.println("----------------------------------");
//lastIndexOf方法与indexOf方法大致相同,只是方向是从右向左进行查找的
System.out.println(s1.lastIndexOf("cd"));
System.out.println(s1.lastIndexOf("cd", 10));
System.out.println(s1.lastIndexOf("cdz"));
System.out.println(s1.lastIndexOf("cdz", 10));
s1 = String.format("%s今年%d岁了,是%s年上的班,工资目前是%f", "张小飞", 32, "2010", 15000.5);
System.out.println(s1);
s1 = String.format("%3$s今年%1$d岁了,是%2$s年上的班,工资目前是%4$f", 32, "2010", "张小飞", 15000.5);
System.out.println(s1);
System.out.printf("%s今年%d岁了,是%s年上的班,工资目前是%f%n", "张小飞", 32, "2010", 15000.5);
System.out.println("abc");
//indet(n)缩进n个空格,并在字符串后加入换行
ystem.out.print("[" + "abc".indent(4) + "]");
System.out.print("[" + "abc".indent(8) + "]");
System.out.println("----------------------------------");
//空串、只是由空白符构成的字符串在调用isBlank()方法后会返回true,否则返回false
System.out.println("".isBlank());
System.out.println(" ".isBlank());
System.out.println("\t\n \r".isBlank());
System.out.println("\t\na\r".isBlank());
String nullStr = null;
//System.out.println(nullStri.isBlank());
//只有字符串的长度是0时,isEmpty()方法才返回true,与字符串内容是否时空白符无关
System.out.println("".isEmpty());
System.out.println(" ".isEmpty());
System.out.println("\t\n \r".isEmpty());
System.out.println("\t\na\r".isEmpty());
System.out.println(" ".trim().isEmpty());
System.out.println(" a b c ".trim().isEmpty());
System.out.println("----------------------------------");
//使用第一个参数作为分隔符,把后续的可变长参数连接成一个字符串
String[] strArr = { "abc", "星垂平野阔", "apple", "3.1415926", "十一不放假", "春假吃卤煮", "Java得永生" };
s1 = String.join(" --- ", strArr);
System.out.println(s1);
s1 = String.join(", ", "abc", "星垂平野阔", "apple", "3.1415926", "十一不放假", "春假吃卤煮", "Java得永生");
System.out.println(s1);
System.out.println("----------------------------------");
System.out.println("abcdefg".length());
System.out.println("星垂平野阔春假吃卤煮".length());
//lines()方法会将字符串以回车换行分隔为若干元素并组织成一个Stream
"abc\nxyz\n123\n春假吃卤煮".lines().forEach(System.out::println);
System.out.println("15263".matches("\\d+"));
System.out.println("-15263".matches("-\\d+"));
System.out.println("abc123XYZ".matches("\\w+"));
System.out.println("\t\n ".matches("\\s+"));
System.out.println("15263".matches("\\D+"));
System.out.println("abc123XYZ".matches("\\W+"));
System.out.println("\t\n ".matches("\\S+"));
System.out.println("----------------------------------");
//str.repeat(n)让str重复n次组成一个新的字符串
s1 = "abc, 星垂平野阔, apple, 3.1415926, 十一不放假, 春假吃卤煮, Java得永生";
strArr = s1.split(", ");
System.out.println(strArr.length);
Arrays.stream(strArr).forEach(System.out::println);
System.out.println("----------------------------------");
strArr = s1.split(", ", 5);
System.out.println(strArr.length);
Arrays.stream(strArr).forEach(System.out::println);
System.out.println("----------------------------------");
System.out.println("[" + " abc ".strip() + "]");
System.out.println("[" + " abc ".trim() + "]");
System.out.println("[" + " ".strip() + "]");
System.out.println("[" + " ".trim() + "]");
System.out.println("[" + " \n ".strip() + "]");
System.out.println("[" + " \n ".trim() + "]");
System.out.println("[" + " abc ".stripLeading() + "]");
System.out.println("[" + " abc ".stripTrailing() + "]");
System.out.println("----------------------------------");
//
s1 = "abcdefghijklmn";
//从s1的第11个字符开始截取到s1的结束
s2 = s1.substring(10);
System.out.println(s2);
//从s1的第四个字符开始截取到第7个字符之前
s2 = s1.substring(3, 6);
System.out.println(s2);
String sb = (String)s1.subSequence(3, 6);
System.out.println(sb);
System.out.println("This Is An Apple.".toUpperCase());
System.out.println("This Is An Apple.".toLowerCase());
}
}
package org.forten.api.date;
import java.util.Date;
public class DateTest001 {
public static void main(String[] args) {
//Date()构造器得到的对象描述着执行此构造器时的提起时间点
Date currentTime = new Date();
System.out.println(currentTime);
long ms = 10 * 365 * 24 * 60 * 60 * 1000L;
//Date(long ms),得到从1970年元旦0点经过ms毫秒后的时间点
Date d = new Date(ms);
System.out.println(d);
//当前时间与1970年元旦0点之间的毫秒差
//new Date(System.currentTimeMillis())与new Date()有作用类似
System.out.println(System.currentTimeMillis());
//getTime()得到提起对象与1970年元旦0点之间的毫秒差
System.out.println(currentTime.getTime());
}
}
package org.forten.api.date;
import java.util.Calendar;
import java.util.Date;
public class DateTest002 {
public static void main(String[] args) {
Calendar cal1 = Calendar.getInstance();
System.out.println(cal1);
cal1.set(2019, 0, 1, 0, 0, 0);
System.out.println(cal1);
cal1.set(Calendar.YEAR, 2010);
cal1.set(Calendar.MONTH, Calendar.AUGUST);
cal1.set(Calendar.DAY_OF_MONTH, 25);
cal1.set(Calendar.HOUR, 3);
cal1.set(Calendar.MINUTE, 15);
cal1.set(Calendar.SECOND, 32);
System.out.println(cal1);
Date date = new Date(20 * 365 * 24 * 60 * 60 * 1000L);
cal1.setTime(date);
System.out.println(cal1);
Calendar cal2 = Calendar.getInstance();
cal2.setTimeInMillis(20 * 365 * 24 * 60 * 60 * 1000L);
System.out.println(cal1.equals(cal2));
cal1.add(Calendar.YEAR, 50);
cal1.add(Calendar.MONTH, -3);
System.out.println(cal1);
}
}