java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

将字符串完全反转的N中方法

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56625

public class splitstr {
   
   
public static void main(String args[])
    {
    String oldstr
="I am a programmer";
    String[] str
= oldstr.split(" ");
    String finalstr
="";
   
for(int i=str.length-1;i>-1;i--)
    {
      finalstr
=finalstr+str[i]+" ";
    }
    System.out.println(finalstr);
    }
}

 

public class tts {
   
   
public static void main(String args[])
    {
    String oldstr
="I am  a   programmer";
    String[] str
= oldstr.split(" ");
    String finalstr
="";
   
for(int i=str.length-1;i>-1;i--)
    {   
     
if(str[i].equalsIgnoreCase(""))
      {
        
continue;
      }
     
if(i==0)
      {
          finalstr
=finalstr+str[i].trim();
         
continue;
      } 
      finalstr
=finalstr+str[i].trim()+" ";
    }
    System.out.println(finalstr);
    }
}

 

 

 

String str = "i am  a   test";

    StringBuilder out
= new StringBuilder(str.length());
    StringBuilder field
= new StringBuilder();
   
   
char[] charArray = str.toCharArray();
   
   
for(int i = charArray.length - 1; i >= 0; i --) {
       
       
if(charArray[i] != ' '){
            field.insert(
0, charArray[i]);
           
        }
else {
           
if(field.length() != 0) {
                out.append(field.toString()) ;
                field
= new StringBuilder();
            }
            out.append(
" ");
        }
    }
   
   
if(field.length() != 0) out.append(field.toString());
    System.out.println(out.toString());

int i = 0;
String oldstr
=" I am  a   programmer";
int length = oldstr.length();

List<String> list = new ArrayList<String>();
//这是以空格开头的情况
if(oldstr.substring(0, 1).equals(" ")) {
    String space
= "";
   
int temp = 0;
   
for(int m = i; m < length; m++) {
       
if(oldstr.codePointAt(m) != 32) {
            temp
= m;
           
break;
        }
    }
    space
= oldstr.substring(i, temp);
    list.add(space);
    i
= temp;
}
while(i < length) {
   
//获取空格之间的单词
    int temp = 0;
    Boolean bool
= false;
    String word
= "";
   
for(int m = i; m < length; m++) {
       
if(oldstr.codePointAt(m) == 32) {
            temp
= m;
            bool
= true;
           
break;
        }
    }
   
if(!bool) {//说明是截取到oldstr的结尾
        temp = length;
    }
    word
= oldstr.substring(i, temp);
    list.add(word);
    i
= temp;
    bool
= false;
   
//获取单词之间的空格
    if(i < length) {
        String space
= "";
           
for(int m = i; m < length; m++) {
                    
if(oldstr.codePointAt(m) != 32) {
                temp
= m;
                bool
= true;
               
break;
            }
        }
       
if(!bool) {
            temp
= length;
        }
        space
= oldstr.substring(i, temp);
        list.add(space);
        i
= temp;
    }
}
String newstr
= "";
for(int j = list.size() - 1; j >= 0; j--) {
    newstr
+= list.get(j);
}
System.out.println(newstr);

 

 

 

public class StringTest {
   
private static String convert(String str) {
       
if (str == null) {
           
return null;
        }

       
char[] cs = str.toCharArray();
       
char[] cs0 = new char[cs.length];
       
int temp = cs.length;
       
       
for (int start = 0; start < cs.length;) {
           
boolean isBlank = isBlank(cs[start]);
           
int end = start;
           
for (; end < cs.length; end++) {
               
if (isBlank != isBlank(cs[end])) {
                   
break;
                }
            }
            System.arraycopy(cs, start, cs0, temp
- (end - start),
                    (end
- start));
            temp
-= (end - start);
            start
= end;
        }
       
return String.valueOf(cs0);
    }

   
private static boolean isBlank(char c) {
       
return c == ' ';
    }

   
public static void main(String[] args) {
        System.out.println(convert(
"1"));
    }
}

 

String oldstr=" I am  a   programmer ";
List
<String> list = new ArrayList<String>();
String regEx1
= "[a-zA-Z]+";
String regEx2
= " +";
Pattern p1
= Pattern.compile(regEx1);
Pattern p2
= Pattern.compile(regEx2);
Matcher m
= Pattern.compile("^ +").matcher(oldstr);
if(m.find()) {
    list.add(m.group());
    oldstr
= m.replaceAll("");
}
while(oldstr.length() > 0) {
    Matcher m1
= p1.matcher(oldstr);
   
if(m1.find()) {
        list.add(m1.group());
        oldstr
= m1.replaceFirst("");   
    }
    Matcher m2
= p2.matcher(oldstr);
   
if(oldstr.length() > 0 && m2.find()) {
        list.add(m2.group());
        oldstr
= m2.replaceFirst("");
    }           
}
String newstr
= "";
for(int j = list.size() - 1; j >= 0; j--) {
    newstr
+= list.get(j);
}
System.out.println(newstr);

 

public class Test {

   
public static void main(String[] args) {
        String str
= "I am  a   programmer";
       
char[] chs = str.toCharArray();
       
for(int i = 0, offset = 0; i < chs.length; i++) {
           
char h = chs[chs.length - 1];
           
if(h == ' ') {
                offset
= i;
            }
           
for(int j = chs.length - 2; j >= offset; j--) {
                chs[j
+ 1] = chs[j];
            }
           
if(h == ' ') {
                chs[i]
= h;
                offset
++;
            }
else {
                chs[offset]
= h;
            }
        }
        System.out.println(chs);
    }
}

posted on 2009-06-10 21:51 rrong_m 阅读(346) 评论(0)  编辑  收藏

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜