一般的for循环字符串反转省略
Java:
StringBuffer的reverse()方法
Collections.reverse()方法
Python:public static void reverseStr(Char[] str) {//先把字符串转数组 List<Char> listStr = Arrays.asList(str); listStr.forEach(System.out::println);//java8流的特性 Collections.reverse(listStr); listStr.forEach(System.out::println); }
列表reverse()方法,改变原列表,无返回值
列表逆序遍历,不改变原列表,返回逆序后的新列表str = "123" listStr = list(str)#字符串转list listStr.reverse() print listStr #['3', '2', '1']
reversed()方法,返回迭代器,需要转为list,不改变原列表str = "123" listStr = list(str) x = listStr[::-1] print x #['3', '2', '1']
str = "123" listStr = list(str) print(list(reversed(listStr)))

本文介绍了使用不同编程语言实现字符串反转的方法,包括Java中的StringBuffer reverse()方法、Collections reverse()方法及Python中的列表reverse()方法、列表逆序遍历及reversed()方法。
1458

被折叠的 条评论
为什么被折叠?



