class StringLianXi
{
public static void sop(String s)
{
System.out.println(s);
}
public static void main(String[] args)
{
String str =" ab cd ";
sop("("+str+")");
//str= myTrim(str);
//sop("("+str+")");
System.out.println("--------------");
sop("("+reverseString(str)+")");//( dc ba )
sop("("+reverseString(str,3,4)+")");//
}
//2.字符串进行反转
/**
思路:1.将字符串变成数组
2.对数组反转
3.将数组变成字符串
*/
public static String reverseString(String s,int start,int end)
{
//将字符串变成数组
char []arr=s.toCharArray();
//对数组反转,包含头尾
reverse(arr,start,end);
//将数组变成字符串
return String.valueOf(arr);//new String (arr);
}
public static String reverseString(String s)
{
return reverseString(s,0,s.length());
}
public static void reverse(char[] arr,int x,int y )
{
for (int start=x,end=y-1;start<end ;start++,end--)
{
swap(arr,start,end);
}
}
/**
private static void reverse(char[]arr)
{
for (int start=0,end=arr.length-1;start<end;start++,end--)
{
swap(arr,start,end);
}
}*/
public static void swap(char[] arr,int x,int y)
{
char temp=arr[x];
arr[x]=arr[y];
{
public static void sop(String s)
{
System.out.println(s);
}
public static void main(String[] args)
{
String str =" ab cd ";
sop("("+str+")");
//str= myTrim(str);
//sop("("+str+")");
System.out.println("--------------");
sop("("+reverseString(str)+")");//( dc ba )
sop("("+reverseString(str,3,4)+")");//
}
//2.字符串进行反转
/**
思路:1.将字符串变成数组
2.对数组反转
3.将数组变成字符串
*/
public static String reverseString(String s,int start,int end)
{
//将字符串变成数组
char []arr=s.toCharArray();
//对数组反转,包含头尾
reverse(arr,start,end);
//将数组变成字符串
return String.valueOf(arr);//new String (arr);
}
public static String reverseString(String s)
{
return reverseString(s,0,s.length());
}
public static void reverse(char[] arr,int x,int y )
{
for (int start=x,end=y-1;start<end ;start++,end--)
{
swap(arr,start,end);
}
}
/**
private static void reverse(char[]arr)
{
for (int start=0,end=arr.length-1;start<end;start++,end--)
{
swap(arr,start,end);
}
}*/
public static void swap(char[] arr,int x,int y)
{
char temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
}