首页 > 程序开发 > 软件开发 > Java >

java 字符串中的每个单词的倒序输出,字符串中以空格分割各个单词,如果碰到数字则跳过

2013-05-17

1.测试类package testcase;import junit.framework.TestCase;import huawei.Demo;public class DemoTest extends TestCase{public void testCase01(){ Demo demo = new Demo(); StringBuffer bu...

1.测试类

package testcase;

import junit.framework.TestCase;
import huawei.Demo;


public class DemoTest extends TestCase
{
public void testCase01()
{
Demo demo = new Demo();
StringBuffer buffer = new StringBuffer();
demo.vConvertMsg("He is a man no12 3456", buffer);
assertEquals("eH si a nam on12 3456", buffer.toString());
}

public void testCase02()
{
Demo demo = new Demo();
StringBuffer buffer = new StringBuffer();
demo.vConvertMsg("1234567890He is a man no12 3456", buffer);
assertEquals("1234567890eH si a nam on12 3456", buffer.toString());
}

public void testCase03()
{
Demo demo = new Demo();
StringBuffer buffer = new StringBuffer();
demo.vConvertMsg("123456H W12345", buffer);
assertEquals("123456H W12345", buffer.toString());
}

public void testCase4()
{

}
}


2.实现类

package huawei;

/**
*
* <[Description]>
* @author yKF51945
* @version 1.0, May 14, 2013
* @since Mobile Office VPN&Centrex BMP
* 编写一个函数,将字符串中的每个单词的倒序输出,字符串中以空格分割各个单词,如果碰到数字则跳过。
详细描述:
示例
输入:He is a man no12 3456
返回:eH si a nam on12 3456
*/
public class Demo
{
public void vConvertMsg(String sInputStr, StringBuffer OutputStr)
{

String[] strs = sInputStr.split(" ");
for (String temp : strs) {
//字符的append
if (temp.matches("^[a-z,A-Z]*$")) {
OutputStr.append(" " + convertString(temp));
} else if (temp.matches("^\\d*$")) {
OutputStr.append(" " + temp);
} else {
OutputStr.append(" ");
String s = temp.replaceAll("\\d", "");
s = convertString(s);
//0-9数字的append
for (int i = 0, j = 0; i < temp.length(); i++) {
char c = temp.charAt(i);
if (!(c >= &#39;0&#39; && c <= &#39;9&#39;)) {
OutputStr.append(s.charAt(j++));
} else {
OutputStr.append(c);
}
}
}

}

String covert = OutputStr.toString().trim();
OutputStr.delete(0, OutputStr.length());
OutputStr.append(covert);
}


// StringBuffer sbm = new StringBuffer ();
// StringBuffer sbn = new StringBuffer ();
//
// String [] spt = sInputStr.split(" ");
//
// String match1 = "^[0-9]+";
//
// Pattern pat2 = Pattern.compile(match1);
//
// for(int i =0;i<spt.length;i++)
// {
// Matcher mat2= pat2.matcher(spt[i]);
//
// if(mat2.find())
// {
// sbm.append(spt[i]);
// }
// else
// {
// sbn.append(spt[i]);
// }
// }
//
// sbn.append(sbm);
//
// if(null!=sInputStr)
// {
//
// String covetr = OutputStr.append(sInputStr).reverse().toString();
//
//// StringBuffer sb = new StringBuffer ();
//// StringBuffer sb2 = new StringBuffer ();
// String [] st = covetr.split(" ");
//
// String match = "^[0-9]+";
//
// Pattern pat = Pattern.compile(match);
//
// for(int s =st.length-1;s>=0;s--)
// {
// Matcher mat = pat.matcher(st[s]);
//
// if(mat.find())
// {
// OutputStr.append(st[s]);
// }
// else
// {
// OutputStr.append(st[s]);
// }
// }
// }
// }

private String convertString(String s)
{
if (s.length() == 1) {
return s;
} else {
return convertString(s.substring(1)) + s.charAt(0);
}
}
}


相关文章
最新文章
热点推荐