java 字符串转数组、数组转字符串、List转数组、数组转List
总结了几种使用jdk中的方法对字符串进行转换(不包含第三方jar包),主要涉及:字符串转数组、数组转字符串、List转数组、数组转List。如果大家有更多或者更好的方式,欢迎留言,谢谢!
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @description: StringTest <br> * @author: heger <br> * @date: 2021/3/20 16:36 <br> */ public class StringTest { public static void main(String[] args) { // 字符串=》数组 3种方法 StringTest stringTest = new StringTest(); String[] stringArr = stringTest.StringToArrayBySplit("1,2,3,5"); System.out.println(Arrays.toString(stringArr)); char[] chars = stringTest.StringToArrayByToCharArray("1235"); System.out.println(Arrays.toString(chars)); String[] stringArr1 = stringTest.StringToArrayByTraverse("1,2,3,5"); System.out.println(Arrays.toString(stringArr1)); // 数组=》字符串 3种方法 String s1 = stringTest.ArrayToString(stringArr); System.out.println(s1); String s2 = stringTest.ArrayToStringByFormat(stringArr); System.out.println(s2); String s3 = stringTest.ArrayToStringTraverse(stringArr); System.out.println(s3); // 数组=》List 2种方法 List<String> stringList2 = stringTest.ArrayToListByAsList(stringArr); stringList2.forEach(e -> System.out.println(e)); List<String> stringList = stringTest.ArrayToListByTraverse(stringArr); stringList.forEach(e -> System.out.println(e)); // List=》数组 2种方法 String[] strings = stringTest.ListToArrayByToArray(stringList2); System.out.println(Arrays.toString(strings)); String[] strings1 = stringTest.ListToArrayByTraverse(stringList); System.out.println(Arrays.toString(strings1)); } /** * @param string * @description: 方法一:字符串=》数组 * @return: java.lang.String[] * @author: heger * @date: 16:41 */ public String[] StringToArrayBySplit(String string) { return string.split(","); } /** * @param string * @description: 方法二:字符串=》数组 * @return: char[] * @author: heger * @date: 16:46 */ public char[] StringToArrayByToCharArray(String string) { return string.toCharArray(); } /** * @param string * @description: 方法三:字符串=>数组 * @return: java.lang.String[] * @author: heger * @date: 2021/3/20 */ public String[] StringToArrayByTraverse(String string) { String[] result = new String[string.length()]; for (int i = 0; i < string.length(); i++) { result[i] = string.charAt(i) + ""; } return result; } /** * @param strings * @description: 方法一:数组=》字符串 * 但=》出来的字符串不是想要的 * 输入:{1, 2, 3, 5} * 预期:12345 * 实际:[1, 2, 3, 5] * @return: java.lang.String * @author: heger * @date: 16:51 */ public String ArrayToString(String[] strings) { return Arrays.toString(strings); } /** * @param strings * @description: 方法二:数组=》字符串 * 缺点:数组中有几个元素,就要写几个%s * @return: java.lang.String * @author: heger * @date: 16:59 */ public String ArrayToStringByFormat(String[] strings) { return String.format("%s%s%s%s", strings); // 数组中有几个元素,就要写几个%s } /** * @param strings * @description: 方法三:数组=》字符串 * @return: java.lang.String * @author: heger * @date: 17:23 */ public String ArrayToStringTraverse(String[] strings) { StringBuilder result = new StringBuilder(); for (String string : strings) { result.append(string); } return result.toString(); } /** * @param strings * @description: 方法一:数组=》List * @return: java.util.List<java.lang.String> * @author: heger * @date: 2021/3/20 */ public List<String> ArrayToListByAsList(String[] strings) { return Arrays.asList(strings); } /** * @param strings * @description: 方法二:数组=》List * @return: java.util.List<java.lang.String> * @author: heger * @date: 2021/3/20 */ public List<String> ArrayToListByTraverse(String[] strings) { List<String> result = new ArrayList<>(); for (String s : strings) { result.add(s); } return result; } /** * @param list list * @description: 方法一:List=》数组 * @return: java.lang.String[] * @author: heger * @date: 2021/3/20 */ public String[] ListToArrayByToArray(List<String> list) { return list.toArray(new String[0]); } /** * @param list * @description: 方法二:List=>数组 * @return: java.lang.String[] * @author: heger * @date: 2021/3/20 */ public String[] ListToArrayByTraverse(List<String> list) { String[] result = new String[list.size()]; for (int i = 0; i < list.size(); i++) { result[i] = list.get(i); } return result; } }