字符串的并置
public class exam {
public static void main(String[] args) {
String hello="你好";
String testOne="你"+"好";
System.out.println("hello"==testOne); //输出的结果是ture
System.out.println("你好"==testOne); //输出的结果是ture
System.out.println("你好"==hello); //输出的结果是ture
String you ="你";
String hi="好";
String testTwo=you+hi;
System.out.println(hello==testTwo); //输出结果是false
String testThree=you+hi;
System.out.println(testTwo==testThree);//输出结果是false
}
}
String类的常用方法
equals的用法
public class exam {
public static void main(String[] args) {
String s1,s2;
s1=new String("天道酬勤");
s2=new String("天道酬勤");
System.out.println(s1.equals(s2)); //输出的结果是true
System.out.println(s1==s2); //输出的结果是true
String s3,s4;
s3="we are student";
s4=new String("we are student");
System.out.println(s3.equals(s4)); //输出结果是true
System.out.println(s3==s4); //输出结果是true
String s5,s6;
s5="勇者无敌";
s6="勇者无敌";
System.out.println(s5.equals(s6));//输出结果是true
System.out.println(s5==s6);//输出结果是true
}
}
Arrays排序
import java.util.*;
public class exam {
public static void main(String[] args) {
String [] a ={"melon","apple","pear","banana"};
String [] b={"西瓜","苹果","梨","香蕉"};
System.out.println("使用SortString类的方法按字典序排列数组a:");
SortString.sort(a);
for(int i=0;i
字符串与基本数据的相互转化
求若干个数之和,若干个数从键盘输入
public class Exam {
public static void main(String[] args) {
double sum=0,item=0;
boolean computable=true;
for(String s:args){
try {
item=Double.parseDouble(s);
sum=sum+item;
}
catch (NumberFormatException e) {
System.out.println("您输入了非数字字符:"+e);
computable=false;
}
}
if(computable)
System.out.println("sum="+sum);
}
}
对象的字符串表示
public class TV {
double price;
public void setPrice(double m) {
price=m;
}
public String toString() {
String oldStr=super.toString();
return oldStr+"\n这是电视机,价格是:"+price;
}
}
public class exam {
public static void main(String[] args) {
TV tv=new TV();
tv.setPrice(5897.95);
System.out.println(tv.toString());
}
}
字符串与字符数组,字节数组
getChars()和toCharArray()方法的使用
public class Exam {
public static void main(String[] args) {
char [] a,c;
String s="1945年8月15日是抗战胜利日";
a=new char[4];
s.getChars(11, 15, a, 0); //数组a的单元一次放的字符是'抗' '战' '胜' '利'
System.out.println(a);
c="十一长假期间,学校都放假了".toCharArray();
for(int i=0;i
public class Exam {
public static void main(String[] args) {
byte d[]="Java你好".getBytes();
System.out.println("数组d的长度是:"+d.length);
String s=new String(d,6,2); //输出 好
System.out.println(s);
s=new String(d,0,6);
System.out.println(s); //输出 Java你
}
}
字符串的加密算法
public class EncryptAndDecrypt {
String encrypt(String sourceString,String password){//加密算法
char [] p=password.toCharArray();
int n=p.length;
char [] c=sourceString.toCharArray();
int m=c.length;
for(int k=0;k
import java.util.*;
public class exam {
public static void main(String[] args) {
String sourceString="今晚十点进攻";
EncryptAndDecrypt person=new EncryptAndDecrypt();
System.out.println("输入密码加密:"+sourceString);
Scanner scanner=new Scanner(System.in);
String password=scanner .nextLine();
String secret=person.encrypt(sourceString, password);
System.out.println("密文:"+secret);
System.out.println("输入解密密码");
password=scanner.nextLine();
String source=person.decyrpt(secret, password);
System.out.println("明文:"+source);
}
}