3 回答
TA贡献1848条经验 获得超6个赞
代码如下.附测试
public class SubStringInverse {
public static String inverseSentence(String in, String sep) {
String[] strs = in.split(sep);
String temp;
int size = strs.length;
// 翻转字符串排列顺序
for (int i = 0; i < size / 2; i++) {
temp = strs[i];
strs[i] = strs[size - 1 - i];
strs[size - 1 - i] = temp;
}
// 重组字符串
String result = strs[0];
for (int i = 1; i < size; i++) {
result += sep;
result += strs[i];
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "123/456/789/987/654/321";
System.err.println("旧:"+str);// 打印原始字符串
System.err.println("新:"+inverseSentence(str, "/"));// 打印操作后的字符串
}
}
输出结果:
旧:123/456/789/987/654/321
新:321/654/987/789/456/123
TA贡献1873条经验 获得超9个赞
public class A
{
public static void main(String[] args)
{
String s="aksd,kasp,akss";
String[] str=s.split(",");
for(int i=str.length-1;i>=0;i--)
{
System.out.println(str[i]);
}
}
}
TA贡献1811条经验 获得超4个赞
String[] arr=in.split(sep);
StringBuilder sb=new StringBuilder();
for(int i=arr.length-1;i>=0;++i){
sb.append(arr[i]);
//sb.append(sep);
}
return sb.toString();
添加回答
举报