Java第十五天學習筆記~常用物件API(Spring類特點、建構函式、常用方法)
常用物件API-----Spring類
一、特點
- 字串是一個特殊的物件
- 字串物件一旦被初始化就不會被改變
package day15; public class StringDemo { public static void main(String[] args) { /* * String類的特點: * 字串物件一旦被初始化就不會被改變。 * */ stringDemo2(); } public static void stringDemo2() { // TODO Auto-generated method stub String s = "abc";//建立一個字串物件在常量池中。 String s1 = new String("abc");//建立兩個物件一個new一個字串物件在堆記憶體中。 System.out.println(s==s1);//false System.out.println(s.equals(s1));//true //string類中的equals複寫Object中的equals建立了string類自己的判斷字串物件是否相同的依據。 //其實就是比較字串內容。 // System.out.println("s="+s); // System.out.println("s1="+s1); } /** * 演示字串定義的第一種方式,並明確字串常量池的特點. * 池中沒有就建立,池中有,直接用。 */ private static void stringDemo1() { String s = "abc";//"abc"儲存在字串常量池中。 // s = "nba"; String s1 = "abc"; System.out.println(s==s1);//true? 都是儲存在常量池中,只是呼叫的時候指向不同的變數,實體內容還是abc // System.out.println("s="+s); } }
二、建構函式
位元組陣列或者字元陣列轉成字串可以通過String類的建構函式完成
//建構函式 package day15; public class StringConstrutorDemo { public static void main(String[] args) { stringConstructorDemo(); } public static void stringConstructorDemo() { // TODO Auto-generated method stub String s=new String();//等效於 String s="";不等效String s=null; byte[] arr= {65,66,67,68}; String s1=new String(arr); System.out.println("s1="+s1); } }
三、String類常用方法
按照面向物件的思想對字串進行功能分類
* "abcd"
*
* 1,獲取:
* 1.1 獲取字串中字元的個數(長度)
* int length();
* 1.2 根據位置獲取字元
* char charAt(int index);
* 1.3 根據字元獲取在字串中的第一次出現的位置
* int indexOf(int ch)
* int indexOf(int ch,int fromIndex):從指定位置進行ch的查詢第一次出現位置
* int indexOf(String str);
* int indexOf(String str,int fromIndex);
* 根據字串獲取在字串中的第一次出現的位置.
* int lastIndexOf(int ch)
* int lastIndexOf(int ch,int fromIndex):從指定位置進行ch的查詢第一次出現位置
* int lastIndexOf(String str);
* int lastIndexOf(String str,int fromIndex);
* 1.4 獲取字串中一部分字串。也叫子串
* String substring(int beginIndex, int endIndex)//包含begin 不包含end
* String substring(int beginIndex);
*
*
*
* 2,轉換
* 2.1 將字串變成字串陣列(字串的切割)
* String[] split(String regex):涉及到正則表示式
* 2.2 將字串變成字元陣列
* char[] toCharArray();
* 2.3 將字串變成位元組陣列
* byte[] getBytes();
* 2.4 將字串中的字母轉成大小寫
* String toUpperCase():大寫
* String toLowerCase():小寫
* 2.5 將字串中的內容進行替換
* String replace(char oldch,char newch);
* String replace(String s1,String s2);
* 2.6 將字串兩端的空格去除
* String trim();
* 2.7 將字串進行連線
* String concat(string);
*
* 3,判斷
* 3.1 兩個字串內容是否相同啊?
* boolean equals(Object obj);
* boolean equalsIgnoreCase(string str);忽略大寫比較字串內容。
* 3.2 字串中是否包含指定字串?
* boolean contains(string str);
* 3.3 字串是否以指定字串開頭 是否以指定字串結尾。
* boolean startsWith(string);
* boolean endsWith(string);
*
* 4,比較
* System.out.println("abc".compareTo("aqz"));
package day15;
public class StringMethodDemo
{
public static void main(String[] args)
{
/*
* 按照面向物件的思想對字串進行功能分類。
* "abcd"
*
* 1,獲取:
* 1.1 獲取字串中字元的個數(長度).
* int length();
* 1.2 根據位置獲取字元。
* char charAt(int index);
* 1.3 根據字元獲取在字串中的第一次出現的位置.
* int indexOf(int ch)
* int indexOf(int ch,int fromIndex):從指定位置進行ch的查詢第一次出現位置
* int indexOf(String str);
* int indexOf(String str,int fromIndex);
* 根據字串獲取在字串中的第一次出現的位置.
* int lastIndexOf(int ch)
* int lastIndexOf(int ch,int fromIndex):從指定位置進行ch的查詢第一次出現位置
* int lastIndexOf(String str);
* int lastIndexOf(String str,int fromIndex);
* 1.4 獲取字串中一部分字串。也叫子串.
* String substring(int beginIndex, int endIndex)//包含begin 不包含end 。
* String substring(int beginIndex);
*
*
*
* 2,轉換。
* 2.1 將字串變成字串陣列(字串的切割)
* String[] split(String regex):涉及到正則表示式.
* 2.2 將字串變成字元陣列。
* char[] toCharArray();
* 2.3 將字串變成位元組陣列。
* byte[] getBytes();
* 2.4 將字串中的字母轉成大小寫。
* String toUpperCase():大寫
* String toLowerCase():小寫
* 2.5 將字串中的內容進行替換
* String replace(char oldch,char newch);
* String replace(String s1,String s2);
* 2.6 將字串兩端的空格去除。
* String trim();
* 2.7 將字串進行連線 。
* String concat(string);
*
* 3,判斷
* 3.1 兩個字串內容是否相同啊?
* boolean equals(Object obj);
* boolean equalsIgnoreCase(string str);忽略大寫比較字串內容。
* 3.2 字串中是否包含指定字串?
* boolean contains(string str);
* 3.3 字串是否以指定字串開頭。是否以指定字串結尾。
* boolean startsWith(string);
* boolean endsWith(string);
*
* 4,比較
* System.out.println("abc".compareTo("aqz"));
*
*/
stringMethodDemo_4();
// System.out.println("abc".concat("kk"));
// System.out.println("abc"+"kk");
// System.out.println(String.valueOf(4)+1);
// System.out.println(""+4+1);
}
private static void stringMethodDemo_4()
{
System.out.println("abc".compareTo("aqz"));
}
private static void stringMethodDemo_3()
{
String s = "abc";
System.out.println(s.equals("ABC".toLowerCase()));
System.out.println(s.equalsIgnoreCase("ABC"));
System.out.println(s.contains("cc"));
String str = "ArrayDemo.java";
System.out.println(str.startsWith("Array"));
System.out.println(str.endsWith(".java"));
System.out.println(str.contains("Demo"));
}
private static void stringMethodDemo_2()
{
String s = "張三,李四,王五";
String[] arr = s.split(",");
for (int i = 0; i < arr.length; i++)
{
System.out.println(arr[i]);
}
char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++)
{
System.out.println(chs[i]);
}
s = "ab你";
byte[] bytes = s.getBytes();
for (int i = 0; i < bytes.length; i++)
{
System.out.println(bytes[i]);
}
System.out.println("Abc".toUpperCase());
String s1 = "java";
String s2 = s1.replace('q', 'z');
System.out.println(s1==s2);//true
System.out.println("-"+" ab c ".trim()+"-");
}
private static void stringMethodDemo_1()
{
String s = "abcdae";
System.out.println("length:"+s.length());//6
System.out.println("char:"+s.charAt(2));//c//StringIndexOutOfBoundsException
System.out.println("index:"+s.indexOf('k'));//0//-1 我們可以根據-1,來判斷該字元或者字串是否存在。
System.out.println("lastIndex:"+s.lastIndexOf('a'));//4
System.out.println("substring:"+s.substring(2,4));
}
}
1,給定一個字串陣列。按照字典順序進行從小到大的排序。
* {"nba","abc","cba","zz","qq","haha"}
/*
* 1,給定一個字串陣列。按照字典順序進行從小到大的排序。
* {"nba","abc","cba","zz","qq","haha"}
*
* 思路:
* 1,對陣列排序。可以用選擇,冒泡都行。
* 2,for巢狀和比較以及換位。
* 3,問題:以前排的是整數,比較用的比較運算子,可是現在是字串物件。
* 字串物件怎麼比較呢?爽了,物件中提供了用於字串物件比較的功能。
*
*
*/
public class StringTest_1 {
/**
* @param args
*/
public static void main(String[] args) {
String[] arr = { "nba", "abc", "cba", "zz", "qq", "haha" };
printArray(arr);
sortString(arr);
printArray(arr);
}
public static void sortString(String[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if(arr[i].compareTo(arr[j])>0)//字串比較用compareTo方法
swap(arr,i,j);
}
}
}
private static void swap(String[] arr, int i, int j) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void printArray(String[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1)
System.out.print(arr[i] + ", ");
else
System.out.println(arr[i] + "]");
}
}
}
2,一個子串在整串中出現的次數。
* "nbaernbatynbauinbaopnba"
/* 2,一個子串在整串中出現的次數。
* "nbaernbatynbauinbaopnba"
* 思路:
* 1,要找的子串是否存在,如果存在獲取其出現的位置。這個可以使用indexOf完成。
* 2,如果找到了,那麼就記錄出現的位置並在剩餘的字串中繼續查詢該子串,
* 而剩餘字串的起始位是出現位置+子串的長度.
* 3,以此類推,通過迴圈完成查詢,如果找不到就是-1,並對 每次找到用計數器記錄。
*
*/
public class StringTest_2 {
/**
* @param args
*/
public static void main(String[] args) {
String str = "nbaernbatnbaynbauinbaopnba";
String key = "nba";
int count = getKeyStringCount_2(str,key);
System.out.println("count="+count);
}
public static int getKeyStringCount_2(String str, String key) {
int count = 0;
int index = 0;
while((index = str.indexOf(key,index))!=-1){
index = index + key.length();
count++;
}
return count;
}
/**
* 獲取子串在整串中出現的次數。
* @param str
* @param key
* @return
*/
public static int getKeyStringCount(String str, String key) {
//1,定義計數器。
int count = 0;
//2,定義變數記錄key出現的位置。
int index = 0;
while((index = str.indexOf(key))!=-1){
str = str.substring(index+key.length());
count++;
}
return count;
}
}
3,兩個字串中最大相同的子串。
/*
* 3,兩個字串中最大相同的子串。
* "qwerabcdtyuiop"
* "xcabcdvbn"
*
* 思路:
* 1,既然取得是最大子串,先看短的那個字串是否在長的那個字串中。
* 如果存在,短的那個字串就是最大子串。
* 2,如果不是呢,那麼就將短的那個子串進行長度遞減的方式去子串,去長串中判斷是否存在。
* 如果存在就已找到,就不用在找了。
*
*
*/
public class StringTest_3 {
/**
* @param args
*/
public static void main(String[] args) {
String s1 = "qwerabcdtyuiop";
String s2 = "xcabcdvbn";
String s = getMaxSubstring(s2, s1);
System.out.println("s=" + s);
}
/**
* 獲取最大子串
*
* @param s1
* @param s2
* @return
*/
public static String getMaxSubstring(String s1, String s2) {
String max = null,min = null;
max = (s1.length()>s2.length())?s1:s2;
min = max.equals(s1)?s2:s1;
System.out.println("max="+max);
System.out.println("min="+min);
for (int i = 0; i < min.length(); i++) {
for(int a = 0,b = min.length()-i; b != min.length()+1; a++,b++){
String sub = min.substring(a, b);
// System.out.println(sub);
if(max.contains(sub))
return sub;
}
}
return null;
}
}
4,模擬一個trim功能一致的方法。
/*
* 4,模擬一個trim功能一致的方法。去除字串兩端的空白
* 思路:
* 1,定義兩個變數。
* 一個變數作為從頭開始判斷字串空格的角標。不斷++。
* 一個變數作為從尾開始判斷字串空格的角標。不斷--。
* 2,判斷到不是空格為止,取頭尾之間的字串即可。
*/
public class StringTest_4 {
/**
* @param args
*/
public static void main(String[] args) {
String s = " ab c ";
s = myTrim(s);
System.out.println("-" + s + "-");
}
public static String myTrim(String s) {
int start = 0, end = s.length() - 1;
while (start <= end && s.charAt(start) == ' ') {
start++;
}
while (start <= end && s.charAt(end) == ' ') {
end--;
}
return s.substring(start, end + 1);
}
}