public static void main(String[] args){ Demo d=new Demo();
String newString=d.trimOne(",,,1,2,3,,,");
System.out.println(newString); }
}// 输出结果是:1,2,3 我用的是递归
import java.util.*;public class Delete { public static String Test(String in) { int n = in.indexOf(","); int m = in.lastIndexOf(","); in = in.substring(n + 1, m); return in; } public static void main(String[] args) { Scanner Jie = new Scanner(System.in); System.out.println("请输入:"); String in = Jie.nextLine(); System.out.println(Test(in)); }}
int len = str.length();
int st = 0;
int off = 0;
char[] val = str.toCharArray(); while ((st < len) && (val[off + st] <= ',')) {
st++;
}
while ((st < len) && (val[off + len - 1] <= ',')) {
len--;
}
return ((st > 0) || (len < str.length())) ? str.substring(st, len)
: str;
}
import java.util.regex.Pattern;public class JpaTest { public static void main(String[] args) {
String[] strs = {"1,2,5,", ",,,1,2,5,", ",,,,,,," };
String regex = "^,*|,*$";
// 需要处理多个字符串
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("");
for(int i = 0; i < strs.length; i++) {
System.out.println(matcher.reset(strs[i]).replaceAll(""));
}
// 只处理一个字符串
String str = ",,,,1,2,3,,,,,";
System.out.println(str.replaceAll(regex, ""));
}
}
public static void main(String[] args) {
String str = ",,,1,2,5,";
String[] strArray = str.split(",");
StringBuilder sb = new StringBuilder();
for(String tmpStr : strArray) {
if(tmpStr.length()>0)
sb.append(tmpStr).append(",");
}
if(sb.toString().endsWith(",")) sb.deleteCharAt(sb.length()-1);
System.out.println(sb.toString());
}
import java.util.regex.Pattern;public class Test03 { public static void main(String[] args) {
String exp = "[^,].+[^,]";
String str = ",,,ad,gc,g,,,,,ggg,,";
Pattern p = Pattern.compile(exp);
Matcher m = p.matcher(str);
while (m.find())
System.out.println(m.group());
}}
public String trimOne(String str){
if(str.startsWith(","))
return trimOne(new String(new StringBuffer(str).deleteCharAt(0)));
else if(str.endsWith(","))
return trimOne(new String(new StringBuffer(str).deleteCharAt(str.length()-1)));
else
return str;
}
public static void main(String[] args){
Demo d=new Demo();
String newString=d.trimOne(",,,1,2,3,,,");
System.out.println(newString);
}
}// 输出结果是:1,2,3
我用的是递归
import java.util.*;public class Delete {
public static String Test(String in) {
int n = in.indexOf(",");
int m = in.lastIndexOf(",");
in = in.substring(n + 1, m);
return in;
} public static void main(String[] args) {
Scanner Jie = new Scanner(System.in);
System.out.println("请输入:");
String in = Jie.nextLine();
System.out.println(Test(in));
}}
public static void main(String[] args) {
String str = ",1,2,3";
StringBuffer strBuf = new StringBuffer();
String [] str2 = str.split(",");
for(int i=0;i<str2.length;i++) {
strBuf.append(str2[i]);
}
System.out.println("output String:" + strBuf.toString());
}
s = s.replaceAll(","," ");
s = s.trim();
s = s.replaceAll(" ", ",");
System.out.println(s);这样很简单的
String s = ",,,12,34,";
System.out.println(s.replaceAll("^,*|,*$", ""));