例子1:
//字符串是一個特殊的對象
public class StringDemo {
public static void main(String[] args) {
String s1 = "abc"; // s1是一個類型變量,“abc”是一個對象,一旦被初始化就不可被改變
String s2 = new String("abc");
String s3 = "abc";
// s1和s2的區別
// 第一句在內存中只有一個對象
// 第二句s2在內存中有兩個對象
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s1 == s3);
// 結果:false
// true
}
}
例子2:
public class StringDemo2 {
public static void test7() {
String s = " Hello word! ";
print(s.toUpperCase()); // 轉成大寫
print(s.toLowerCase()); // 轉換小寫
print(s.trim()); // 去掉首尾空格
String s1 = "abc";
String s2 = "abb";
print(s1.compareTo(s2)); // 比較兩個字符串
}
public static void sub() {
String a = "abcdef";
print(a.substring(2)); // 從指定位置開始到結尾,如果下標不存在,會出現字符串下標越界異常
print(a.substring(2, 4)); // 包含頭,不包含尾,
}
public static void split() {
String str = "tomcat,apache,java";
String[] arr = str.split(",");
for (String a : arr)
print(a);
}
public static void replace_1() {
String str = "hello java";
String s1 = str.replace("java", "wrod");
print(s1);
}
public static void change() {
char arr[] = { 'a', 'b', 'c', 'd', 'e' };
String str = new String(arr);
print(str);
char arr2[] = str.toCharArray();
for (int i = 0; i <= arr2.length; i++)
print(arr2[i]);
print(str);
}
/*
* 文件名的判斷
*/
public static void is() {
String str = "StringDemo.java";
// 判斷文件名是否以String開頭
print(str.startsWith("String"));
// 判斷文件是不是.java格式
print(str.endsWith(".java"));
// 判斷文件中時候含有Demo字符串內容
print(str.contains("Demo"));
}
/*
* String的獲取方法
*/
public static void get() {
String str = "abcdekakf";
// 獲取字符串指定下標的字符
print(str.charAt(5)); // 當下標超出字符串的長度時,或拋出StringIndexOutOfBoundsException異常
print(str.length()); // 字符串長度
print(str.indexOf("dek")); // 獲取指定字符所在位子的下標 當字符串中沒有指定的字符時,輸出-1
print(str.indexOf("a", 3)); // 從指定位子3開始進行查找指定字符下標 當字符串中沒有指定的字符時,輸出-1
print(str.lastIndexOf("a")); // 從字符串的後面進行開始查找指定字符的位子
}
public static void main(String[] args) {
test7();
// split();
// change();
// replace_1();
// is();
// get();
}
// 打印輸出方法 使用的喲
public static void print(Object obj) {
System.out.println(obj);
}
}
/*
public class MyStrimDemo {
public static void main(String[] args) {
String str = " hello word ";
System.out.println(mytrim(str));
}
public static String mytrim(String str) {
int start = 0, end = str.length() - 1;
// 分析: 我們先拿字符串前面部分爲空格的最後一個空格下標
while (start <= end && str.charAt(start) == ' ')
start++;
// 分析: 我們先拿字符串後面部分爲空格的最後一個空格下標
while (start <= end && str.charAt(end) == ' ')
end--;
return str.substring(start, end + 1);
}
}
public class DIYString {
public static void main(String[] args) {
// String str = "0123456789";
// print(overturn(str));
// print(overturn(str, 0, 5));
// String str = "abeeeeeeeabkkkkkkkkab";
// print(getSubCount(str, "ab"));
String s1 = "gegeasgHellogege";
String s2 = "aaaaHelloooo";
print(getMaxSub(s2, s1));
}
// 3. 獲取兩個字符串中最大相同子串
public static String getMaxSub(String s1, String s2) {
String max = "";
String min = "";
max = s1.length() > s2.length() ? s1 : s2;
min = max == s1 ? s2 : s1;
for (int i = 0; i < min.length(); i++) {
for (int j = 0, x = min.length() - i; x != min.length() + 1; j++, x++) {
String temp = min.substring(j, x);
if (max.contains(temp))
return temp;
}
}
return null;
}
// 2.計算指定字符串出現次數 這種方式消耗資源小
public static int getSubCount(String str, String key) {
int index = 0;
int count = 0;
while ((index = str.indexOf(key)) != -1) {
str = str.substring(index + key.length());
count++;
}
return count;
}
// 選擇翻轉
public static String overturn(String str, int start, int end) {
char[] cha = str.toCharArray();
return new String(reversalArr(cha, start, end));
}
// 全部字符串反轉
public static String overturn(String str) {
char[] cha = str.toCharArray();
return String.valueOf(reversalArr(cha, 0, cha.length - 1));
}
// 翻轉數組
public static char[] reversalArr(char[] arr, int start, int end) {
for (int i = start, j = end; i <= j; i++, j--) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
// 打印
public static void print(Object obj) {
System.out.println(obj);
}
}
StringBuffer例子1:
public class StringBufferDemo {
/**
* @param args
*/
public static void main(String[] args) {
// add();
// delete();
// update();
reverse();
}
// 翻轉測試
public static void reverse() {
StringBuffer buf = new StringBuffer("123456789");
buf.reverse();
print(buf.toString());
}
public static void update() {
StringBuffer buf = new StringBuffer("123456789");
buf.replace(1, 3, "**"); // 替換不包含end位置
print(buf.toString());
buf.setCharAt(8, '0');
print(buf.toString());
}
public static void delete() {
StringBuffer buf = new StringBuffer("123456789");
buf.delete(1, 3); // 當下標超出時,會拋出下標越界異常
print(buf.toString());
buf.deleteCharAt(0); // 刪除第一個字符
print(buf.toString());
}
public static void add() {
StringBuffer buffer = new StringBuffer();
buffer.append("gege").append(false).append(123);
print(buffer.toString());
buffer.insert(1, "**");
print(buffer.toString());
}
// 打印
public static void print(Object obj) {
System.out.println(obj);
}
}