你什么时候搞JAVA了?
我一直搞的都是J2EE
兔子不吃窝边草
[CODE]/*
* Test.java
*
* Created on 2007年8月2日, 上午9:45
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author hadeslee
*/
public final class Test {
/** Creates a new instance of Test */
private Test() {
}
public static String encode(String input){
StringBuilder sb=new StringBuilder();
for(int i=0;i<input.length();i++){
char c=input.charAt(i);
if(Character.isDigit(c)){
if(c==48){
sb.append(c);
}else{
try{
char temp=input.charAt(i+1);
int sum=c-48;
for(int j=0;j<sum+1;j++){
sb.append(temp);
}
}catch(StringIndexOutOfBoundsException exe){
sb.append(c);
}
}
}else if(c=='_'){
sb.append("\\UL");
}else{
sb.append(c);
}
if(i!=input.length()-1){
sb.append("_");
}
}
return sb.toString();
}
public static String decode(String input){
StringBuilder sb=new StringBuilder();
int fromIndex=0;
int lastIndex=input.lastIndexOf('_');
for(int i=0;i<input.length();i++){
char c=input.charAt(i);
if(c=='_'){
if(isValid(input,i)){
String sub=input.substring(fromIndex,i);
fromIndex=i+1;
sb.append(parseString(sub));
}
if(i==lastIndex){
String sub=input.substring(lastIndex+1,input.length());
sb.append(parseString(sub));
}
}
}
return sb.toString();
}
private static String parseString(String s){
if(s.length()==1){
return s;
}else if(s.equals("\\UL")){
return "_";
}else{
return ""+(s.length()-1);
}
}
private static boolean isValid(String input,int index){
try{
char temp=input.charAt(index-1);
if(temp!='_'){
return true;
}
}catch(Exception exe){
}
try{
char temp=input.charAt(index+1);
if(temp!='_'){
return true;
}
}catch(Exception exe){
}
return false;
}
public static void main(String[] args) {
String s=Test.encode("\\_b2_\\U2asdf3458b__s");
System.out.println(s);
System.out.println(Test.decode(s));
}
}[/CODE]
你看一下,行不行
我试了几个,好像没出错