视频
笔记
1.1字符串拼接
需求一:返回n个字符串拼接结果,如果没有传入字符串,那么返回空字符串””
public class Demo02Var { public static void main(String[] args) { String result = concat("张无忌", "张翠山", "张三丰", "张三"); System.out.println("result = " + result); } public static String concat(String...s){ String str = ""; for (int i = 0; i < s.length; i++) { str+=s[i]; } return str; } }
需求二:n个字符串进行拼接,每一个字符串之间使用某字符进行分隔,如果没有传入字符串,那么返回空字符串""
比如:concat("-","张三丰","张翠山","张无忌") -> 返回 -> 张三丰-张翠山-张无忌
public class Demo03Var { public static void main(String[] args) { String result = concat("-", "张三丰", "张翠山", "张无忌"); System.out.println("result = " + result); } public static String concat(String regex, String... s) { String str = ""; for (int i = 0; i < s.length; i++) { if (i == s.length - 1) { str += s[i]; } else { str += s[i] + regex; } } return str; } }