原來String類型這麼強大,之前只是覺得它是定義一個字符串用的。接下來就是通過做作業去學習String類型的一天了。
文末有JDK API 1.6中文版幫助文檔,新手必備,免費獲取!哥哥我可是找了好久。
1.用代碼演示String類中的以下方法的用法
(1)boolean isEmpty(): 判斷字符串是不是空串,如果是空的就返回true
(2)char charAt(int index): 返回索引上的字符
(3)String toLowerCase(): 字符串轉成小寫
(4)String toUpperCase(): 字符串轉成大寫
(5)String repalce(char oldChar, char newChar): 將字符串中的老字符,替換爲新字符
(6)String repalce(String old, String newstr): 將字符串中的老字符串,替換爲新字符串
(7)String trim(): 去掉字符串兩端空格
我就一個一個測試了,把所有的功能全寫在了一個類中。注意點都寫在了代碼註釋中,這個還是很簡單的。
package cn.demo01;
/*
* 解釋一下以下注釋()後面是什麼意思。例如(1)boolean isEmpty(): 判斷字符串是不是空串,如果是空的就返回true
* 全是String類型的方法介紹 返回值 方法名 方法功能描述
* 這種解釋格式是Java API的中文幫助文檔,這個文檔我的博客中有,免費獲取!
*/
public class stringDemo {
public static void main(String[] args) {
function_1();
function_2();
function_3();
function_4();
function_5();
function_6();
function_7();
}
/*
* (7)String trim(): 去掉字符串兩端空格
*/
public static void function_7(){
String s = new String(" acvsjavadfasf ");
System.out.println("(7)例"+s);
String s1 = s.trim();
System.out.println(""+s1);
}
/*
* (6)String repalce(String old, String newstr): 將字符串中的老字符串,替換爲新字符串
*/
public static void function_6(){
String s = new String("acvsjavadfasf");
String s1 = s.replace("java","AAAaaa");
System.out.println("(6)"+s1);
}
/*
* (5)String repalce(char oldChar, char newChar): 將字符串中的老字符,替換爲新字符
*/
public static void function_5(){
String s = new String("acvsjavadfasf");
String s1 = s.replace('j', '6'); //只能替換一個字符,記得加‘’
System.out.println("(5)"+s1);
}
/*
* (4)String toUpperCase(): 字符串轉成大寫
*/
public static void function_4(){
String s = new String("acvsdfasf");
String s1 = s.toUpperCase();
System.out.println("(4)"+s1);
}
/*
* (3)String toLowerCase(): 字符串轉成小寫
*/
public static void function_3(){
String s = new String("ABCDEFG");
String s1 = s.toLowerCase();
System.out.println("(3)"+s1);
}
/*
* (2)char charAt(int index): 返回索引上的字符
*/
public static void function_2(){
String s = new String("abcdef");
char c = s.charAt(3);
System.out.println("(2)"+c);
}
/*
* (1)boolean isEmpty(): 判斷字符串是不是空串,如果是空的就返回true
*/
public static void function_1(){
String s = new String("123456");
boolean b = s.isEmpty();
System.out.println("(1)"+b);
}
}
2.分析以下需求,並用代碼實現:
(1)定義如下方法public static String getPropertyGetMethodName(String property);
(2)該方法的參數爲String類型,表示用戶給定的成員變量的名字,返回值類型爲String類型,返回值爲成員變量對應的get方法的名字
(3)如:用戶調用此方法時給定的參數爲"name",該方法的返回值爲"getName"
我沒搞懂這個題目的意義在哪。可能是我把題目理解錯了
package cn.demo02;
/*
* 分析以下需求,並用代碼實現:
(1)定義如下方法public static String getPropertyGetMethodName(String property);
(2)該方法的參數爲String類型,表示用戶給定的成員變量的名字,返回值類型爲String類型,返回值爲成員變量對應的get方法的名字
(3)如:用戶調用此方法時給定的參數爲"name",該方法的返回值爲"getName"
*/
public class SetMethod {
public static void main(String[] args) {
System.out.println(getPropertyGetMethodName("張三"));
}
public static String getPropertyGetMethodName(String property){
String name = property;
return name;
}
}
3.分析以下需求,並用代碼實現:
(1)定義數字字符串數組{"010","3223","666","7890987","123123"}
(2)判斷該數字字符串數組中的數字字符串是否是對稱(第一個數字和最後一個數字相等,第二個數字和倒數第二個數字是相等的,依次類推)的,並逐個輸出
(3)如:010 是對稱的,3223 是對稱的,123123 不是對稱的
(4)最終打印該數組中對稱字符串的個數
提示:循環獲取字符串的每一個字符,依次比較第一個和最後一個,第二個和倒數第二個。。。
代碼我是咋看咋對,可結果就是少一個。
package cn.demo03;
/*
* (1)定義數字字符串數組{"010","3223","666","7890987","123123"}
(2)判斷該數字字符串數組中的數字字符串是否是對稱(第一個數字和最後一個數字相等,第二個數字和倒數第二個數字是相等的,依次類推)的,並逐個輸出
(3)如:010 是對稱的,3223 是對稱的,123123 不是對稱的
(4)最終打印該數組中對稱字符串的個數
提示:循環獲取字符串的每一個字符,依次比較第一個和最後一個,第二個和倒數第二個。。。
*/
public class Judge {
public static void main(String[] args) {
function();
}
public static void function(){
String[] st = {"010","3223","666","7890987","123123"};
int count = 0;
int c = 0;//最終打印個數,要用到計數器
for(int i = 0;i < st.length;i++){
String s = st[i];
for(int j = 0;j <s.length()/2;j++){
if(s.charAt(j) != s.charAt((s.length()-1))-j){
count =0;
}else {
count = 1;
}
}
if(count == 1){
System.out.println(st[i]); //打印出每個對稱字符串
}
c = c+count;
}
System.out.println("對稱字符串個數: "+c);
}
}
JDK API 1.6 中文版:
鏈接:鏈接:https://pan.baidu.com/s/1OWVWlHXnzzw9M8E8IfsTkg
提取碼:o0oj