1. 程式人生 > >字串的迴圈左移&迴圈右移

字串的迴圈左移&迴圈右移

直接上程式碼的了,感興趣的可以看看,挺有意思的:)

package cn.edu.ccit.fangwh;

import org.junit.Test;

public class StringShift {
	public String left(String s,int n){
		n=n%s.length();
		return (s+s).substring(n, n+s.length());
	}
	
	public String right(String s,int n){
		return left(s,s.length()-n%s.length());
	}
	@Test
	public void test(){
		String left=left("asdasdff",1);
		String right=right("asdasdff",1);
		System.out.println(left+"  "+right);
	}
}

結果:

sdasdffa  fasdasdf