String类的对象是不可变的,有时使用StringBuilder/StringBuffer类,可以改变字符串的内容,然后最终通过tostring方法将其转化为string类
明暗文转化:
package first;
import java.net.URLEncoder;
import javax.swing.JOptionPane;
public class FilterAndDececrypt {
static String information="abcdefghijklmnopqrstuvwxyz";//
static String cryptograph="XNYAHPOGZQWBTSFLRCVMUEKJDI";
public static void main(String[] args) {
String s=JOptionPane.showInputDialog("please input a string");
String filter_s=filter(s);
String info=decrypt(filter_s);
String output="mingwen is: "+info;
JOptionPane.showConfirmDialog(null, output);
}
// get the chart 'A'to 'Z'
public static String filter(String s)
{
StringBuffer strBuf=new StringBuffer();//build a Stringbuffer
for (int i=0;i<s.length();i++){
if(s.charAt(i)>'A'&&s.charAt(i)<'Z'){
strBuf.append(s.charAt(i));//add to the strbuffer
}//get the chart from A to Z
}
return strBuf.toString();
}
//encode
public static String decrypt(String s){
StringBuffer strBuf=new StringBuffer();//build a void string buffer
int i=0;
while(i<s.length()){
char code=s.charAt(i); //get the char
int index=cryptograph.indexOf(code);//get the location of code
char ele=information.charAt(index);
strBuf.append(ele);
i++;
}
return strBuf.toString();
}
}