class StringText
{
public static void main(String[] args)
{
String s = " abc dfe ";
sop(s);
//String s1=myTrim(s);
//sop(s1);
reverseString(s,3,6);
}
public static void sop(String str)
{
System.out.println(str);
}
public static String myTrim(String str)
{
int start = 0 , end = str.length()-1;
while (start<=end && str.charAt(start)==' ')
{
start++;
}
while (start<=end && str.charAt(end)==' ')
{
end--;
}
return str.substring(start,end+1);
}
//反转字符串
//将字符串转成字符数组
//将字符数组反转
//将字符数组转成字符串。
public static String reverseString(String str,int start,int end)
{
char[] chs = str.toCharArray();
reverse();
return new String(chs);
}
public static void reverse(char[] arr,int x,int y)
{
for (int start=x,int end=y-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;
}
}
|
|