JAVA中实现字符返转的方法

package com;



public class Str {

	

	/**

	 * 使用String.toCharArray方法

	 * @param oldString

	 * @return

	 */

	static public String reverString1(String oldString)

	{

		String resultString ="";

		

		char [] charArray = oldString.toCharArray();

		

		for(int i=charArray.length-1 ;i>=0;i--)

		{

			resultString += charArray[i];

		}

		

		return resultString;

	}

	

	

   /**

    * 利用StringBuffer的方法

    * @param oldString

    * @return

    */

	static public String reverString2(String oldString)

	{

		

		

		 StringBuffer strBuffer = new StringBuffer(oldString);

		return strBuffer.reverse().toString();

		

	}

	

	/**

	 * 递归截取实现字符返转

	 * @param oldString

	 * @return

	 */

	public static String reverString3(String oldString)

	{

	  String resultString="";

	   if(oldString.length()==1)

	   {



		   return oldString;

	   }

	   else

	   {

		   String subString1 = oldString.substring(0, oldString.length()-1);    

           String subString2 = oldString.substring(oldString.length()-1);    



             resultString +=subString2;  

          return resultString+ reverString3 (subString1);       

	   }

	}

	

	public static void main(String[] args) {

		

		String oldString = "Hello World!";

     

	    System.out.print(oldString.startsWith("Hello"));

	}



}

你可能感兴趣的:(java)