接上一篇文章
1.字符串比较与字符串地址比较
public class ArithmeticoperatorDem1 {
public static void main(String[] args) {
char[]chs={'a','b','c'};
String s1=new String(chs);
String s2=new String(chs);
System.out.println(s1==s2);
String s3="abc";
String s4="abc";
System.out.println(s3==s4);
System.out.println(s1==s3);
}
}
2.使用equals()函数比较两个字符串的内容是否相同
public class ArithmeticoperatorDem2 {
public static void main(String[] args) {
char[]chs={'a','b','c'};
String s1=new String(chs);
String s2=new String(chs);
String s3="abc";
String s4="abc";
System.out.println(chs);
System.out.println(s1==s3);
System.out.println(s1==s2);
System.out.println(s3==s4);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s3.equals(s4));
}
}
3.逐个字母遍历输入的字符串
public class ArithmeticoperatorDem3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line=sc.nextLine();
for(int i=0;i<line.length();i++){
System.out.println(line.charAt(i));
}
}
}
4.循环遍历记录字符串中大写字母,小写字母与数字出现的次数
public class ArithmeticoperatorDem4 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串");
String line=sc.nextLine();
int bigcount=0;
int smalcount=0;
int numbercount=0;
for(int i=0;i<line.length();i++){
char a=line.charAt(i);
System.out.println();
if(a>='A'&&a<='Z'){
bigcount+=1;
} else if (a>='a'&&a<='z') {
smalcount+=1;
} else if (a>='0'&&a<='9') {
numbercount+=1;
}
}
System.out.println("大写字母出现的次数为:"+bigcount);
System.out.println("小写字母出现的次数为:"+smalcount);
System.out.println("数字出现的次数为:"+numbercount);
}
}
5.循环遍历字符串
public class ArithmeticoperatorDem5 {
public static void main(String[] args) {
int arr[]={1,2,3};
String s=arrayToString(arr);
System.out.println(s);
}
public static String arrayToString(int[]arr){
String s="";
s+="[";
for(int i=0;i<arr.length;i+=1){
if(i==arr.length-1){
s+=arr[i];
}else {
s+=arr[i];
s+=",";
}
}
s+="]";
return s;
}
}
6.字符串反转案例
public class ArithmeticoperatorDem6 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串");
String line=sc.nextLine();
String s=reverse(line);
System.out.println("反转后");
System.out.println(s);
}
public static String reverse(String s){
String ss="";
for(int i=s.length()-1;i>=0;i--){
ss+=s.charAt(i);
}
return ss;
}
}
7.StringBuilder案例
public class ArithmeticoperatorDem7 {
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
System.out.println("sb:"+sb);
System.out.println(sb.length());
StringBuilder sb2=new StringBuilder("hello");
System.out.println("sb2:"+sb2);
System.out.println(sb2.length());
}
}
8.StringBuilder特性
public class ArithmeticoperatorDem8 {
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
StringBuilder sb2=sb.append("hello");
System.out.println("sb"+sb);
System.out.println("sb2:"+sb2);
System.out.println(sb==sb2);
sb.reverse();
System.out.println(sb);
}
}
9.String和StringBuilder相互转化
public class ArithmeticoperatorDem9 {
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
sb.append("hello");
String s=sb.toString();
System.out.println(s);
String e="hello11";
StringBuilder s2=new StringBuilder(e);
System.out.println(s2);
}
}
10.String转换为StringBuilder调用reverse方法最后转回String
public class ArithmeticoperatorDem10 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line=sc.nextLine();
String s = myReverse(line);
System.out.println("s:"+s);
}
public static String myReverse(String s){
StringBuilder sb=new StringBuilder(s);
sb.reverse();
String ss= sb.toString();
return ss;
}
}