日期:2014-05-20 浏览次数:20857 次
public class test {
static String s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String s2 = "ysG3MHPZdLx5vO1N0TeRtcmljkziK4bECWoIpuJ62AVrBUDnX9SgFYq7wfh8aQ";
public char getSwap(char c) {
int stat = s1.indexOf(c);
char cc = s2.charAt(stat);
return cc;
}
public String swap(String s) {
String ss = "";
for (int i = 0; i < s.length(); i++) {
ss += getSwap(s.charAt(i));
}
return ss;
}
public static void main(String args[]) {
test t = new test();
System.out.println(t.swap("0123456789"));
}
}
import java.util.*;
public class ChangeStringWithString2
{
public static final String s1="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String s2="ysG3MHPZdLx5vO1N0TeRtcmljkziK4bECWoIpuJ62AVrBUDnX9SgFYq7wfh8aQ";
private Map<Character, Character> map;
public ChangeStringWithString2()
{
map=getMap(s1,s2);
}
private Map<Character, Character> getMap(String s1,String s2)
{
Map<Character, Character> map = new HashMap<Character,Character>();
for(int i=0;i<s1.length();i++)
{
char c1= s1.charAt(i);
Character cChar1=new Character(c1);
char c2=s2.charAt(i);
Character cChar2=new Character(c2);
map.put(cChar1,cChar2);
}
return map;
}
public String change(String s)
{
StringBuilder sb=new StringBuilder(s);
for(int i=0;i<s.length();i++)
{
Character cs=new Character(sb.charAt(i));
Character cs2=map.get(cs);
if(cs2==null)
{
continue;
}
else
{
sb.setCharAt(i,cs2);
}
}
return sb.toString();
}
public static void main(String[] args)
{
ChangeStringWithString2 csws=new ChangeStringWithString2();
String s="abcd123 456";
String result=csws.change(s);
System.out.println("befor change s is "+s);
System.out.println("after change s is "+result);
}
}
public static void main(String[] args) {
String s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String s2 = "ysG3MHPZdLx5vO1N0TeRtcmljkziK4bECWoIpuJ62AVrBUDnX9SgFYq7wfh8aQ";