字串 String 練習

2021-06-26 15:33:18 字數 1269 閱讀 2929

練習一:模擬trim()方法

public class test 

public static string mytrim(string str)

/*先進行角標判斷(start不能超過end);再進行字串中角標為end的字元是否為' '

*如果為空格,則end的角標向前一步,知道start角標的字元部位空格,start角標自減停止

* */

while(start <= end && str.charat(end) == ' ')

/*將字串中角標為start與end之間的子串擷取出來,注意包含頭,不會包含尾,end要加1*/

return str.substring(start,end+1);

}}

練習二:將字串反轉

public class test 

public static string reversestring(string str)

public static void reverse(char arr)

public static string reversestring(string str,int start,int end)

public static string reversestring(string str)

public static void reverse(char arr,int x,int y)

public static int getsubcount(string str,string key)

return count;

}}

練習四:獲取兩個字串中相同的最大字串

首先:要對長度較短的字串進行長度依次遞減的子串列印

public class test 

public static string getmaxsubstring(string s1,string s2)

} return "";

}}

注意: 大圈套小圈

原理中,最外層的遞增是:整個字串要減去的字串的長度(第一輪要

減去0個,得到

乙個子串

,然後對這乙個子串進行判斷;第二輪要

減去乙個,得到

兩個子串

,然後對這兩個子串進行判斷;第三輪要

減去兩個

,得到 三個子串

,然後對這三個子串分別進行判斷、、、、、、)

字串String類小練習

1 把陣列中的資料按照指定個格式拼接成乙個字串 舉例 int arr 輸出結果 1,2,3 分析 a 定義乙個int型別的陣列 b 寫方法實現把陣列中的元素按照指定的格式拼接成乙個字串 c 呼叫方法 d 輸出結果 public class stringtest 寫方法實現把陣列中的元素按照指定的格式...

對字串String的測試練習

字串的測試練習,與君共勉!不要見笑.public class teststring public static void stringtest 判斷乙個字串在另乙個字元中第一次出現的位置 string sf a int index sa.indexof sf system.out.println 4...

筆記 string 字串

字串定義 char buffer 128 char buffer 128 hello world sizeof 函式 取得定義 長度 128,字串有效長度 11 char buffer hello world sizeof 函式取得定義的 長度 12,字串有效長度 11 字串賦值 strcpy ch...