字符串的反转

package sc.zdd.test;

public class StrTest {
/**
* 字符串的反转
* @param str要返转后的字符
* @return 返转后的字符
*/
public static String fz(String str){
char[] value = str.toCharArray();
int count = str.length();
int n = count-1;
for(int j=(n-1)>>1;j>=0;--j){
char temp = value[j];
char temp2 = value[n-j];
value[n-j]=temp;
value[j] = temp2;
}
return new String(value);
}
/**
* 一句话中的,各个单词反转
* @param str
* @return
*/
public static String fzjz(String str){
String[] value = str.trim().split(" ");
int n = value.length;
System.out.println(n);
for(int i =0;i<n;i++){
str=str.replace(value[i], fz(value[i]));
}
return  str;
}
/**
  * 这个是java.lang.String中的反转方法
  * @param str
  * @return
  */
public static String reverse(String str) {
char[] value = str.toCharArray();
int count = str.length();
boolean hasSurrogate = false;
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; --j) {
    char temp = value[j];
    char temp2 = value[n - j];
    System.out.println(temp+0);
    System.out.println(temp2+0);
    if (!hasSurrogate) {
hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
    || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
    }
    value[j] = temp2;
    value[n - j] = temp;
}
if (hasSurrogate) {
    // Reverse back all valid surrogate pairs
    for (int i = 0; i < count - 1; i++) {
char c2 = value[i];
if (Character.isLowSurrogate(c2)) {
    char c1 = value[i + 1];
    if (Character.isHighSurrogate(c1)) {
value[i++] = c1;
value[i] = c2;
    }
}
    }
}
return new String(value);
    }
/**
* 这个是为了实现中文占两个字符的截取
* @param str 要截取的母字符
* @param n 是要截取的前几个字符
* @return
*/
public  static String subString(String str,int n){
char[] value = str.toCharArray();
char[] temp  = new char[n];
int count = value.length;
if(count==0||n==0){
return "";
}
for(int i=0;i<n;i++){
if(value[i]>256){
n--;
}
if(i<n){
temp[i]=value[i];
}

}
return new String(temp);
}
public static void main(String[] args) {
// Boolean b1 = new Boolean("sss");
// System.out.println(b1);
// //false这里只在这个字符串只有是true时才会返回true,其余都是返回false
// System.out.println(Math.abs(-100));//100
// System.out.println(Math.abs(Integer.MAX_VALUE));//2147483647
// System.out.println(Math.abs(Integer.MIN_VALUE));//-2147483648
// StringBuffer sb1 = new StringBuffer("a");
// sb1.append("b");
// System.out.println(sb1.reverse());
// System.out.println(sb1);
// String s1 = "aaa";
// s1.concat("bbbbb");
// System.out.println(s1);
// System.out.println(fzjz("  ab  中d国m sd  "));
System.out.println(subString("中国",0));
}
}

你可能感兴趣的:(J#)