字符串
拼接
"a" + 1
1 + "a"
比较
package com. atguigu. Common_classes ;
public class demo01_string {
public static void main ( String [ ] args) {
String s1 = "a" ;
String s2 = "A" ;
System . out. println ( s1. equals ( s2) ) ;
System . out. println ( s1. equalsIgnoreCase ( s2) ) ;
int res = s1. compareTo ( s2) ;
int Ignres = s1. compareToIgnoreCase ( s2) ;
System . out. println ( res) ;
System . out. println ( Ignres ) ;
byte b1 = ( byte ) 'a' ;
byte b2 = ( byte ) 'A' ;
System . out. println ( "a = " + b1) ;
System . out. println ( "A = " + b2) ;
}
}
输出:
截断
package com. atguigu. Common_classes ;
public class demo02 {
public static void main ( String [ ] args) {
String temp = "Hello World" ;
System . out. println ( temp. substring ( 6 , temp. length ( ) ) ) ;
System . out. println ( temp. substring ( 6 ) ) ;
String [ ] temps = temp. split ( " " ) ;
System . out. println ( temps. length) ;
for ( int i = 0 ; i < temps. length; i++ ) {
System . out. println ( temps[ i] ) ;
}
String s = " Hello World " ;
System . out. println ( "!" + s. trim ( ) + "!" ) ;
}
}
替换
package com. atguigu. Common_classes ;
public class demo03 {
public static void main ( String [ ] args) {
String temp1 = "Hello World" ;
System . out. println ( temp1. replace ( "World" , "cen" ) ) ;
String temp2 = "Hello World1 World2" ;
System . out. println ( temp2. replaceAll ( "World1|World2" , "cen" ) ) ;
}
}
大小写
package com. atguigu. Common_classes ;
public class demo04 {
public static void main ( String [ ] args) {
String temp = "Hello World" ;
System . out. println ( temp. toLowerCase ( ) ) ;
System . out. println ( temp. toUpperCase ( ) ) ;
}
}
查找
package com. atguigu. Common_classes ;
public class demo05 {
public static void main ( String [ ] args) {
String temp1 = "World Hello World" ;
System . out. println ( temp1. indexOf ( "World" ) ) ;
System . out. println ( temp1. indexOf ( "World" , 12 ) ) ;
System . out. println ( temp1. lastIndexOf ( "World" ) ) ;
System . out. println ( temp1. contains ( "World" ) ) ;
System . out. println ( temp1. startsWith ( "World" ) ) ;
System . out. println ( temp1. endsWith ( "World" ) ) ;
System . out. println ( temp1. isEmpty ( ) ) ;
}
}
StringBuilder
package com. atguigu. Common_classes ;
public class demo06 {
public static void main ( String [ ] args) {
StringBuilder temp = new StringBuilder ( ) ;
System . out. println ( temp. append ( "abc" ) ) ;
System . out. println ( temp. reverse ( ) ) ;
System . out. println ( temp. insert ( 1 , "d" ) ) ;
}
}
包装类
package com. atguigu. Common_classes. DataType ;
public class demo01 {
public static void main ( String [ ] args) {
Byte b = null ;
Short s = null ;
Integer i = null ;
Long l = null ;
Float f = null ;
Double d = null ;
Character c = null ;
Boolean bln = null ;
int temp = 10 ;
Integer i1 = temp;
int i2 = i1;
}
}
日期类
package com. atguigu. Common_classes. Date ;
import java. text. ParseException ;
import java. text. SimpleDateFormat ;
import java. util. Date ;
public class demo01 {
public static void main ( String [ ] args) {
System . out. println ( System . currentTimeMillis ( ) ) ;
Date temp = new Date ( ) ;
System . out. println ( temp) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss.sss" ) ;
String dateFormatString = sdf. format ( temp) ;
System . out. println ( dateFormatString) ;
String dateString = "2022-06-01 00:00:00.000" ;
Date parseDate = null ;
try {
parseDate = sdf. parse ( dateString) ;
} catch ( ParseException e) {
throw new RuntimeException ( e) ;
}
System . out. println ( parseDate) ;
System . out. println ( parseDate. before ( temp) ) ;
System . out. println ( parseDate. after ( temp) ) ;
}
}
日历类
package com. atguigu. Common_classes. Calender ;
import java. util. Calendar ;
import java. util. Date ;
public class demo01 {
public static void main ( String [ ] args) {
Calendar instance = Calendar . getInstance ( ) ;
System . out. println ( instance. get ( Calendar . YEAR ) ) ;
System . out. println ( instance. get ( Calendar . MONTH ) ) ;
System . out. println ( instance. get ( Calendar . DATE ) ) ;
instance. setTime ( new Date ( ) ) ;
instance. add ( Calendar . YEAR , - 1 ) ;
}
}
打印日历
package com. atguigu. Common_classes. Calender ;
import java. util. Calendar ;
public class demo02 {
public static void main ( String [ ] args) {
System . out. println ( "周一\t周二\t周三\t周四\t周五\t周六\t周日" ) ;
Calendar now = Calendar . getInstance ( ) ;
int maxDay = now. getActualMaximum ( Calendar . DAY_OF_MONTH ) ;
now. set ( Calendar . DAY_OF_MONTH , 1 ) ;
for ( int i = 1 ; i <= maxDay; i++ ) {
int weekX = now. get ( Calendar . DAY_OF_WEEK ) ;
if ( i == 1 ) {
if ( weekX == Calendar . SUNDAY ) {
for ( int j = 0 ; j < 6 ; j++ ) {
System . out. print ( "\t" ) ;
}
} else {
for ( int j = 0 ; j < weekX- 2 ; j++ ) {
System . out. print ( "\t" ) ;
}
}
}
System . out. print ( i) ;
if ( weekX == Calendar . SUNDAY ) {
System . out. println ( ) ;
} else {
System . out. print ( "\t" ) ;
}
now. add ( Calendar . DATE , 1 ) ;
}
}
}
工具类
package com. atguigu. Common_classes. Util ;
import java. text. SimpleDateFormat ;
import java. util. Date ;
import java. util. Random ;
import java. util. UUID ;
public class demo01 {
public static void main ( String [ ] args) {
System . out. println ( StringUtil . isEmpty ( null ) ) ;
System . out. println ( StringUtil . isEmpty ( "" ) ) ;
System . out. println ( StringUtil . isEmpty ( " " ) ) ;
System . out. println ( StringUtil . isEmpty ( "abc" ) ) ;
System . out. println ( StringUtil . makeString ( ) ) ;
System . out. println ( StringUtil . makeString ( "abcdef" , 6 ) ) ;
System . out. println ( StringUtil . formatDate ( new Date ( ) , "yyyy-MM-dd" ) ) ;
try {
System . out. println ( StringUtil . parseDate ( "2023-02-02" , "yyyy-MM-dd" ) ) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
}
}
class StringUtil {
public static boolean isEmpty ( String str)
{
if ( str == null || "" . equals ( str. trim ( ) ) ) {
return true ;
}
return false ;
}
public static boolean isNotEmpty ( String str)
{
return ! isEmpty ( str) ;
}
public static String makeString ( )
{
return UUID . randomUUID ( ) . toString ( ) ;
}
public static String makeString ( String from, int len)
{
if ( len < 1 ) {
return "" ;
} else {
char [ ] chars = from. toCharArray ( ) ;
StringBuilder str = new StringBuilder ( ) ;
for ( int i = 0 ; i < len; i++ ) {
Random random = new Random ( ) ;
int j = random. nextInt ( chars. length) ;
char c = chars[ j] ;
str. append ( c) ;
}
return str. toString ( ) ;
}
}
public static String transfrom ( String source, String encodeFrom, String encodeTo) throws Exception
{
byte [ ] bytes = source. getBytes ( encodeFrom) ;
return new String ( bytes, encodeTo) ;
}
public static Date parseDate ( String dateString, String format) throws Exception
{
SimpleDateFormat sdf = new SimpleDateFormat ( format) ;
return sdf. parse ( dateString) ;
}
public static String formatDate ( Date date, String format)
{
SimpleDateFormat sdf = new SimpleDateFormat ( format) ;
return sdf. format ( date) ;
}
}
比较
package com. atguigu. Common_classes . compare;
public class demo01 {
public static void main ( String [ ] args) {
int num1 = 10 ;
int num2 = 10 ;
System . out. println ( "p1 " + ( num1 == num2) ) ;
double num3 = 10.0 ;
System . out. println ( "p2 " + ( num1 == num3) ) ;
String s1 = "abc" ;
String s2 = "abc" ;
String s3 = new String ( "abc" ) ;
System . out. println ( "p3 " + ( s1 == s2) ) ;
System . out. println ( "p4 " + ( s1 == s3) ) ;
System . out. println ( "p5 " + ( s1. equals ( s3) ) ) ;
Temp temp1 = new Temp ( ) ;
Temp temp2 = new Temp ( ) ;
System . out. println ( "p6 " + ( temp1 == temp2) ) ;
System . out. println ( "p7 " + ( temp1. equals ( temp2) ) ) ;
Integer i1 = Integer . valueOf ( 128 ) ;
Integer i2 = Integer . valueOf ( 128 ) ;
System . out. println ( "p8 " + ( i1 == i2) ) ;
System . out. println ( "p9 " + ( i1. equals ( i2) ) ) ;
}
}
class Temp {
@Override
public int hashCode ( ) {
return 1 ;
}
@Override
public boolean equals ( Object obj) {
return true ;
}
}
参考
尚硅谷. 【尚硅谷】7天搞定Java基础,Java零基础极速入门