字符串倒序输出

java实现:

//利用String类的toCharArray(),再倒序输出数组。
public class javatest {
	public static void main(String[] args) {
		String originalString = "abcdefg";
		String resultString = "";
		char[] charArray = originalString.toCharArray();
		for (int i = charArray.length-1; i>=0; i--){
			resultString +=charArray[i];
		}
		System.out.println(resultString);
}
}

//使用reverse()方法
public class reverseTest {
	public static void main(String[] args) {
		String originalString = "abcdefg";
		StringBuffer stringBuffer = new StringBuffer(originalString);
		System.out.println(stringBuffer.reverse());
}
}

Python实现:

>>> foostr = 'abcde'
>>> foostr[::-1]
'edcba'
>>> foostr[::-2]
'eca'
>>> foolist = [123, 'xba', 342.23, 'abc']
>>> foolist[::-1]
['abc', 342.23, 'xba', 123]



你可能感兴趣的:(java,String,python,Class)