String.equals()实现代码:
String类中的equals()方法:
public boolean equals(Object anObject)
{
if (this == anObject)
{
return true;
} if (anObject instanceof String)
{
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count)
{
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0)
{
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
Length()使用说明:
用来返回字符串的长度
实例:
String s1 = "Hello World";
System.out.println("s1's length is: " + s1.length());
运行结果:
s1's length is: 11
charAt()使用说明:
返回制定索引处的char值,索引范围从到0到length()-1
String s1 = "Hello World";
System.out.println(S1.charAt(2));
运行结果:l;
Gethar使用说明:获得从指定位置起的子串复制到字符串组中
char[] S1={‘H’,’e’,’l’,’l’,’o’,’ ‘,’m’,’a’,’n’};
String S2=new String(World);
S2.getChar(0,6,S1,7);
System.out.println(S2);
运行结果:Hello World;
Replace()使用说明:子串替换
String S1=”Hmllo World”;
System.out.println(S1.replace(‘m’,’e’);
运行结果:Hello World;
toUpperCase() toLowerCase()使用说明:大小写转换
String s1 = "Hello World";
System.out.println(S1.toUpperCase());
System.out.println(S1.toLowerCase());
运行结果:HELLO WORLD
Hello world;
Trim()使用说明:去除头尾空格
String s1 = " Hello World ";
System.out.println(S1.trim());
运行结果:Hello World;
ToCharArray()使用说明:将字符串赋给数组
字符加密程序:
设计思路:获取字符串中每个字符的,每个字符后移三位加密,X,Y,Z变为A,B,C,解密时每个字符前移三位,A,B,C变为X,Y,Z
程序流程图
开始 |
输入一个字符串 |
选择要进行的操作 |
解密操作 |
加密操作 |
输出操作后的结果 |
结束 |
解密 加密
程序代码://ZhangZuoxiu
//2015 10 23
package demo;
import java.util.Scanner;//调用类
public class Secret {
void kd()
{
Scanner in = new Scanner(System.in);
System.out.println("请选择操作(1.加密 2.解密):");
int n=in.nextInt();
if(n == 1)
{
System.out.println("请输入要加密的字符串:");
String S1 = in.next();
String S2="";
int key = 3;//凯撒密码加密,向后移位3位
for(int i = 0;i < S1.length();i++)
{
char c = S1.charAt(i);//将S1的每个位上的字母赋给数组
if(c >= 'a'&&c <= 'z')
{
if(c>='x'&&c<='z')//某个字符为x,y,z
{
c-=26;
c+=key;
}
else c+=key;
}
else if(c >= 'A'&&c <= 'Z')
{
if(c>='X'&&c<='Z')//某个字符为X,Y,Z
{
c-=26;
c+=key;
}
else c+=key;
}
S2 += c;
}
System.out.println("加密后的字符串是:"+S2);
System.out.println("输入任意建继续,0结束程序:");
n=in.nextInt();
if(n==0)
{
System.out.print("谢谢使用");
}
else
{
this.kd();
}
}
else if(n == 2)
{
System.out.println("输入要解密的字符串:");
String str = in.next();
String S4="";
int key = -3;//凯撒密码解密,向前移位3位
for(int i = 0;i < str.length();i++)
{
char c = str.charAt(i);
if(c >= 'a'&&c <= 'z')
{
if(c>='a'&&c<='c')//某个字符为a,b,c
{
c+=26;
c+=key;
}
else c+=key;
}
else if(c >= 'A'&&c <= 'Z')
{
if(c>='A'&&c<='C')//某个字符为A,B,C
{
c+=26;
c+=key;
}
else
{
c+=key;
}
}
S4 += c;
}
System.out.println("解密后的字符串:"+S41
);
System.out.println(" 输入任意建继续,0结束程序:");
n=in.nextInt();
if(n==0)
{
System.out.print(" 谢谢使用本程序,欢迎再次使用!");
}
else
{
this.kd();
}
}
else
{
System.out.println("请输入1或2,其他字符无效!");
System.out.println("输入任意建继续,0结束程序:");
n=in.nextInt();
if(n==0)
{
System.out.println("谢谢使用");
}
else
{
this.kd();
}
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Secret S3=new Secret();
S3.kd();
}
}
结果:请选择操作(1.加密 2.解密):
1
请输入要加密的字符串:
dsad
加密后的字符串是:gvdg
输入任意建继续,0结束程序:
0
谢谢使用