当前位置: 代码迷 >> 综合 >> encode() decode()实现对字符串的变换还原
  详细解决方案

encode() decode()实现对字符串的变换还原

热度:68   发布时间:2024-01-16 16:49:31.0

 

(2007-03-27 16:50:55)   人生何处相逢
实现两个方法encode() decode()实现对字符串的变换还原。


1。若当前字符串非数字字符或为数字字符0,则复制改字符串于新字符串中
2。若已知字符串的当前字符串是一个数字字符,且它之后没有后继字符,则简单的将它复制倒新字符串中。
3。若已知字符串的当前字符是一个大于0的数字字符,并且还有后继字符,设该数字字符的面值为N,则将
   它的后继字符(包括后继字符是一个数字字符)重复复制N+1次到新字符串中。
4。以上述一次变换为一组,在不同组之间另插一个下划线‘—’用于分隔;
5。若已知字符串中包含有下划线‘—’,则变换为用‘/UL’.

   如Encode()原24ab_2t2的变换为444_aaaaa_a_b_/UL_ttt_t_2

public String retu()
{
String newStr;
int len = str.length();
for(int i =0;i<len;i++)
{
 String s = str.substring(i,i);
   if(!Character.isDigit(s)||s.equals("0"))
  {
       newStr = newStr + s;
  }else if(s.equals("_"))
  {
     newStr = newStr + "|UL";
  }
  else
  {
      if(i>=len)
      {
        newStr = newStr + s;
      }else
      {
        if(Integer.ParseInt(s) > 0)
        {
            if(Character..isDigit(str.substring(i+1,i+1)))
            for(int j=0;j<Integer.ParseInt;j++)
            {
               newStr = newStr + str.substring(i+1,i+1);
            }
            str = str.replace(s,"N");
        }
       
      }
  }
}
return newStr;


 (2007-03-27 17:07:04)     
public class Main {
 public static void main(String[] args) {
  String str = null;
  if (args.length == 0) {
   System.exit(0);
  } else {
   str = args[0];
  }
  StringBuffer buf = new StringBuffer();
  for (int i = 0; i < str.length(); i++) {
   char c = str.charAt(i);
   if ((c + "").equals("_")) {
    buf.append("//UI");
   } else if (!Character.isDigit(c) || (c + "").equals("0")) {
    buf.append(c);
   } else if (i != str.length() - 1) {
    String s = c + "";
    int n = Integer.parseInt(s);
    for (int k = 0; k < n; k++) {
     buf.append(str.charAt(i + 1));
    }
    buf.append("_");
   }
   else
    buf.append(c);
  }
  System.out.println(buf);
 }
}

  相关解决方案