## 題1:寫一個函數,任意輸入一個字符串,如果兩個字符之間有多個空格字符,那么僅保留一個空格字符,例如“abc??? abc”,輸出“abc?abc”。
~~~
/**
* 返回一個字符串,將原有字符串中多個連續空格只保留一個
* @param s
* @return
*/
public static String checkSpace (String s) {
// \s 匹配的是任意的空白符,包括空格,制表符(Tab),換行符,中文全角空格
return s.replaceAll("\\s{1,}", " ");
}
~~~
## 題2:寫一個函數,輸出一個文件夾下所有文件的名稱
~~~
/**
* 輸出一個文件夾下所有的文件名稱
* @param path
*/
public static void outputFiles(String path) {
File parent = new File(path);
if(!parent.exists())
{
System.out.println("path is not exist");
return;
}
File[] childs=parent.listFiles();
for(int i=0;i<childs.length;i++){
if(childs[i].isFile()){
System.out.println(childs[i].getName());
}
}
}
~~~
本文將持續更新