Java字符串与包装类
生活随笔
收集整理的這篇文章主要介紹了
Java字符串与包装类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/**
?* 字符串與包裝類
?*/
package com.org.lxh;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
?* @author Administrator
?*
?*/
public class Chp8 {
/*
* 驗證電話號碼
*/
public static String checkPhoneNumber(String text){
if(text == null || text.isEmpty()){
return "error";
}
//定義正則表達(dá)式
String regex = "^\\d{3}-?\\d{8}\\d{4}-?\\d{8}$";
if(text.matches(regex))
return "電話號碼";
else
return "不是的";
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//1、語句 "3+5="+3+5 輸出什么?
System.out.println("3+5="+3+5);
//2、String str = null; 與 String str=""; 的區(qū)別是什么?
/*
* 前一句沒有分配內(nèi)存,后一句創(chuàng)建了一個長度為0的空字符串。并在內(nèi)存中為其分配了內(nèi)存空間。
*?
*/
//3、String類是否可以被繼承?
/*
* 不可以
*?
*/
//4、字符串大小寫的轉(zhuǎn)換?
/*
*?
*?
*/
String str = "Long long ago, There's girl,She's name little REDHAT!";
String str2 = str.toUpperCase();
String str3 = str.toLowerCase();
System.out.println(str + "\n" + str2 + "\n" + str3);
//5、字符串的反向輸出?
/*
*?
*?
*?
*/
//6、== 運(yùn)算符與equals()比較對象的區(qū)別是什么?
/*
* 當(dāng)使用運(yùn)算符 “==” 比較兩個對象時,是比較兩個對象使用的內(nèi)存地址和內(nèi)容是否相同,如果兩個對象使用的是同一個地址,
* 并且內(nèi)容相同,則結(jié)果為true,否則結(jié)果為false。
*?
* 當(dāng)使用equals()比較兩個對象時,則是比較兩個對象的內(nèi)容是否相同,而與對象的內(nèi)存地址無關(guān)。
*?
*/
String str11 = new String("hello");
String str22 = new String("hello");
String str33 = str22; //str22與str33同一個內(nèi)存地址
String str44 = "hello";
//使用 == 比較str11和str22
System.out.println("str1 == str2 比較的結(jié)果為: \t" + (str11 == str22)); //false
//使用 == 比較str22和str33
System.out.println("str22 == str33 比較的結(jié)果為:\t" + (str22 == str33)); //true
//
System.out.println("str22 == str44 的比較結(jié)果為:\t" + (str22 == str44)); //false
//使用equals比較str11和str22
System.out.println("str11.equals(str22)的結(jié)果為:\t" + str11.equals(str22)); //true
//使用equals比較str22和str33
System.out.println("str22.equals(str33)的結(jié)果為:\t" + str22.equals(str33)); //true
//7、判斷字符串是否為空的方式?
/*
*?
*?
*?
*/
String val = null;
if(val == null)
System.out.println("String val is null");
val = "";
if(val.equals(""))
System.out.println("String val is equals \"\"");
if(val.length() == 0)
System.out.println("String val's length is zero");
//8、如何實(shí)現(xiàn)你對日期的格式化?
/*
*?
* JDK1.5開始,String類的format()方法可以格式化日期變量
*?
* %td 一個月的第幾天
* %te 一個月的第幾天
* %tm 月份
* %ty 年份 ??
* %tY 4位年份
* %tj 一年中的第幾天
* %ta %tA 星期
* %tb %tB 月份
*?
*/
Date date = new Date();
System.out.println("默認(rèn)日期格式:" + date);
System.out.println("一個月的哪一天: " + String.format("%te", date));
System.out.println("星期全稱: " + String.format("%tA", date));
//9、時間變量格式化
/*
* %tH 兩位24小時制的小時
* %tI 兩位12小時的小時
* %tk
* %tl
* %tM 分鐘
* %tS 秒
* %tL 毫秒
* %tN 毫微秒
* %tp am,pm
* %tZ 時區(qū)
* %ts 自1970-01-01 00:00:00到現(xiàn)在經(jīng)過的秒數(shù)
* %tQ 毫秒數(shù)
*?
*/
System.out.println("24小時制的小時: " + String.format("%tk", date));
System.out.println("時區(qū): " + String.format("%tZ", date));
//10、日期與時間的組合格式化
/*
* %tR
* %tT
* %tr ??
* %tD ? =%tm%td%ty ? 02/28/2012
* %tF ? ISO 8601格式的日期
* %tc
*/
System.out.println("24小時的時間: " + String.format("%tR", date));
System.out.println("24小時的時間: " + String.format("%tT", date));
System.out.println("12小時制的時間: " + String.format("%tr", date));
System.out.println("英文寫法的日期: " + String.format("%tD", date));
System.out.println("ISO 8601日期的格式: " + String.format("%tF", date));
System.out.println(">>: " + String.format("%tc", date));
//11、常規(guī)數(shù)據(jù)類型的格式化?
/*
* 類似C語言
*?
* %b ? %B ?布爾類型
* %c ? %C ?字符串
* %s ? %S
* %d ? ?%o ? ?%x ?%X
* %e ? ?%E
* %% ? %符號
*/
System.out.println("浮點(diǎn)數(shù): " + String.format("%E", 128000.56f));
System.out.println("浮點(diǎn)數(shù): " + String.format("%e", 128000.56f));
System.out.println("整數(shù): " + String.format("%d", 7756));
System.out.println("八進(jìn)制整數(shù): " + String.format("%o", 7756));
System.out.println("十六進(jìn)制整數(shù): " + String.format("%x", 7756));
System.out.println("十六進(jìn)制整數(shù): " + String.format("%X", 7756));
//12、正則表達(dá)式
/*
* String類的matches()方法
*?
*/
String num = "0431-3385926";
System.out.println(num + " " + checkPhoneNumber(num));
//驗證IP地址, 下面使用正則表達(dá)式
/*
* ?[abc] 表示a、b和c
* ?[^abc] 表示a、b和c除外
* ?[a-zA-Z] 字符集
* ?[a-d[m-p]] a-d或m-p
* ?[a-z&&[def]] d e f
* ?[a-z&&[^bc]] ? a-z中排除b c
* ?[a-z&&[^m-p]]?
*/
Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
Matcher matcher = pattern.matcher("127.400.600.2"); //以驗證127.400.600.2為例
System.out.println(matcher.matches());
System.out.println("=================================================================");
//日期
Pattern pattern1 = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
Matcher matcher1 = pattern1.matcher("2000-02-29 23:59:59");
System.out.println(matcher1.matches());
//郵箱
Pattern pattern2 = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
Matcher matcher2 = pattern2.matcher("hemmingway-@163.com");
System.out.println(matcher2.matches());
//13、字符串和數(shù)值類型的轉(zhuǎn)換?
/*
*?
*/
String s1 = "100";
String s2 = "253";
Integer int1 = Integer.parseInt(s1);
Integer int2 = Integer.parseInt(s2);
int ret = int1 + int2;
System.out.println("變量1: " + int1 + " ?變量2:" + int2 + " ?的和為:" + ret);
//14、去掉字符串中的空格
/*
*?
*/
String s3 = "fdg dsl dfgds ? sdngds dsgdss";
String s4 = s3.replaceAll(" ", ""); ? ? ?//第一個參數(shù)是正則表達(dá)式子
System.out.println(s3 + ", " + s4);
//15、子字符串
/*
* substring
*?
*/
String testStr = "lkssldf2011dsfgd36";
char[] chars = testStr.toCharArray();
boolean bool = Character.isDigit(chars[7]);
if(bool == true)
System.out.println("字符串: " + testStr + " 包含數(shù)字");
//16、
}
}
?* 字符串與包裝類
?*/
package com.org.lxh;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
?* @author Administrator
?*
?*/
public class Chp8 {
/*
* 驗證電話號碼
*/
public static String checkPhoneNumber(String text){
if(text == null || text.isEmpty()){
return "error";
}
//定義正則表達(dá)式
String regex = "^\\d{3}-?\\d{8}\\d{4}-?\\d{8}$";
if(text.matches(regex))
return "電話號碼";
else
return "不是的";
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//1、語句 "3+5="+3+5 輸出什么?
System.out.println("3+5="+3+5);
//2、String str = null; 與 String str=""; 的區(qū)別是什么?
/*
* 前一句沒有分配內(nèi)存,后一句創(chuàng)建了一個長度為0的空字符串。并在內(nèi)存中為其分配了內(nèi)存空間。
*?
*/
//3、String類是否可以被繼承?
/*
* 不可以
*?
*/
//4、字符串大小寫的轉(zhuǎn)換?
/*
*?
*?
*/
String str = "Long long ago, There's girl,She's name little REDHAT!";
String str2 = str.toUpperCase();
String str3 = str.toLowerCase();
System.out.println(str + "\n" + str2 + "\n" + str3);
//5、字符串的反向輸出?
/*
*?
*?
*?
*/
//6、== 運(yùn)算符與equals()比較對象的區(qū)別是什么?
/*
* 當(dāng)使用運(yùn)算符 “==” 比較兩個對象時,是比較兩個對象使用的內(nèi)存地址和內(nèi)容是否相同,如果兩個對象使用的是同一個地址,
* 并且內(nèi)容相同,則結(jié)果為true,否則結(jié)果為false。
*?
* 當(dāng)使用equals()比較兩個對象時,則是比較兩個對象的內(nèi)容是否相同,而與對象的內(nèi)存地址無關(guān)。
*?
*/
String str11 = new String("hello");
String str22 = new String("hello");
String str33 = str22; //str22與str33同一個內(nèi)存地址
String str44 = "hello";
//使用 == 比較str11和str22
System.out.println("str1 == str2 比較的結(jié)果為: \t" + (str11 == str22)); //false
//使用 == 比較str22和str33
System.out.println("str22 == str33 比較的結(jié)果為:\t" + (str22 == str33)); //true
//
System.out.println("str22 == str44 的比較結(jié)果為:\t" + (str22 == str44)); //false
//使用equals比較str11和str22
System.out.println("str11.equals(str22)的結(jié)果為:\t" + str11.equals(str22)); //true
//使用equals比較str22和str33
System.out.println("str22.equals(str33)的結(jié)果為:\t" + str22.equals(str33)); //true
//7、判斷字符串是否為空的方式?
/*
*?
*?
*?
*/
String val = null;
if(val == null)
System.out.println("String val is null");
val = "";
if(val.equals(""))
System.out.println("String val is equals \"\"");
if(val.length() == 0)
System.out.println("String val's length is zero");
//8、如何實(shí)現(xiàn)你對日期的格式化?
/*
*?
* JDK1.5開始,String類的format()方法可以格式化日期變量
*?
* %td 一個月的第幾天
* %te 一個月的第幾天
* %tm 月份
* %ty 年份 ??
* %tY 4位年份
* %tj 一年中的第幾天
* %ta %tA 星期
* %tb %tB 月份
*?
*/
Date date = new Date();
System.out.println("默認(rèn)日期格式:" + date);
System.out.println("一個月的哪一天: " + String.format("%te", date));
System.out.println("星期全稱: " + String.format("%tA", date));
//9、時間變量格式化
/*
* %tH 兩位24小時制的小時
* %tI 兩位12小時的小時
* %tk
* %tl
* %tM 分鐘
* %tS 秒
* %tL 毫秒
* %tN 毫微秒
* %tp am,pm
* %tZ 時區(qū)
* %ts 自1970-01-01 00:00:00到現(xiàn)在經(jīng)過的秒數(shù)
* %tQ 毫秒數(shù)
*?
*/
System.out.println("24小時制的小時: " + String.format("%tk", date));
System.out.println("時區(qū): " + String.format("%tZ", date));
//10、日期與時間的組合格式化
/*
* %tR
* %tT
* %tr ??
* %tD ? =%tm%td%ty ? 02/28/2012
* %tF ? ISO 8601格式的日期
* %tc
*/
System.out.println("24小時的時間: " + String.format("%tR", date));
System.out.println("24小時的時間: " + String.format("%tT", date));
System.out.println("12小時制的時間: " + String.format("%tr", date));
System.out.println("英文寫法的日期: " + String.format("%tD", date));
System.out.println("ISO 8601日期的格式: " + String.format("%tF", date));
System.out.println(">>: " + String.format("%tc", date));
//11、常規(guī)數(shù)據(jù)類型的格式化?
/*
* 類似C語言
*?
* %b ? %B ?布爾類型
* %c ? %C ?字符串
* %s ? %S
* %d ? ?%o ? ?%x ?%X
* %e ? ?%E
* %% ? %符號
*/
System.out.println("浮點(diǎn)數(shù): " + String.format("%E", 128000.56f));
System.out.println("浮點(diǎn)數(shù): " + String.format("%e", 128000.56f));
System.out.println("整數(shù): " + String.format("%d", 7756));
System.out.println("八進(jìn)制整數(shù): " + String.format("%o", 7756));
System.out.println("十六進(jìn)制整數(shù): " + String.format("%x", 7756));
System.out.println("十六進(jìn)制整數(shù): " + String.format("%X", 7756));
//12、正則表達(dá)式
/*
* String類的matches()方法
*?
*/
String num = "0431-3385926";
System.out.println(num + " " + checkPhoneNumber(num));
//驗證IP地址, 下面使用正則表達(dá)式
/*
* ?[abc] 表示a、b和c
* ?[^abc] 表示a、b和c除外
* ?[a-zA-Z] 字符集
* ?[a-d[m-p]] a-d或m-p
* ?[a-z&&[def]] d e f
* ?[a-z&&[^bc]] ? a-z中排除b c
* ?[a-z&&[^m-p]]?
*/
Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
Matcher matcher = pattern.matcher("127.400.600.2"); //以驗證127.400.600.2為例
System.out.println(matcher.matches());
System.out.println("=================================================================");
//日期
Pattern pattern1 = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
Matcher matcher1 = pattern1.matcher("2000-02-29 23:59:59");
System.out.println(matcher1.matches());
//郵箱
Pattern pattern2 = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
Matcher matcher2 = pattern2.matcher("hemmingway-@163.com");
System.out.println(matcher2.matches());
//13、字符串和數(shù)值類型的轉(zhuǎn)換?
/*
*?
*/
String s1 = "100";
String s2 = "253";
Integer int1 = Integer.parseInt(s1);
Integer int2 = Integer.parseInt(s2);
int ret = int1 + int2;
System.out.println("變量1: " + int1 + " ?變量2:" + int2 + " ?的和為:" + ret);
//14、去掉字符串中的空格
/*
*?
*/
String s3 = "fdg dsl dfgds ? sdngds dsgdss";
String s4 = s3.replaceAll(" ", ""); ? ? ?//第一個參數(shù)是正則表達(dá)式子
System.out.println(s3 + ", " + s4);
//15、子字符串
/*
* substring
*?
*/
String testStr = "lkssldf2011dsfgd36";
char[] chars = testStr.toCharArray();
boolean bool = Character.isDigit(chars[7]);
if(bool == true)
System.out.println("字符串: " + testStr + " 包含數(shù)字");
//16、
}
}
總結(jié)
以上是生活随笔為你收集整理的Java字符串与包装类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《流浪地球 2》李雪健人类股骨演讲发布,
- 下一篇: 小米13哭晕了 雷军偏心:相较而言 小米