3.手机号的加密:(字符串截取)
基本格式:
s.substring(a,b); //截取字符串s从a到b,但包括a不包括b; s.substring(c); //截取从c索引开始到末尾的所有元素
import java.sql.SQLOutput; import java.util.*; import java.awt.*; public class hello { public static void main(String []avgs) { String s="16639058447"; String s1=s.substring(0,3); String s2=s.substring(7); String result=s1+"****"+s2; System.out.println(result); } }
4.拼接字符串StringBulider()容器
定义:
SringBuilder sb=new StringBulider()
添加字符串:
sb.append(a);
获取长度:
sb.length()
转换成字符串
sb.toString();
倒序
sb.reveser();
展示:
public class SquenceListTest { public static void main(String []avgs) { StringBuilder sb=new StringBuilder("傻瓜"); //创建对象 sb.append(1); //进行添加 sb.append(1.2); sb.append(true); sb.reverse(); //进行翻转输出 System.out.println(sb); int length=sb.length(); //计算合并后的长度 System.out.println(length); String s=sb.toString(); //把容器转换成字符串 System.out.println(s); } }
4.1举列子:
public class SquenceListTest { public static void main(String []avgs) { int []arr=new int[]{1,2,3}; String s=arrTo(arr); System.out.println(s); } public static String arrTo(int []arr){ StringBuilder sb=new StringBuilder(); System.out.print("["); for(int i=0;i<arr.length;i++){ if(i==arr.length-1){ sb.append(arr[i]).append("]"); }else{ sb.append(arr[i]).append(","); } } String s=sb.toString(); return s; } }
5.新时代拼接符号StringJoiner()
基本格式:
StringJoiner(间隔符号)
代码展示:
StringJoiner(“间隔符号”,"开始符号","结束符号")
import java.sql.SQLOutput; import java.util.*; import java.awt.*; public class hello { public static void main(String []avgs) { StringJoiner sj=new StringJoiner(",","[","]"); sj.add("aaa").add("bbb").add("ccc"); System.out.println(sj); } }
添加(只能是字符串)
sj.add();
代码展示:
import java.sql.SQLOutput; import java.util.*; import java.awt.*; public class hello { public static void main(String []avgs) { StringJoiner sj=new StringJoiner("---"); sj.add("aaa").add("bbb").add("ccc"); System.out.println(sj); } }
长度:
sj.length();
import java.sql.SQLOutput; import java.util.*; import java.awt.*; public class hello { public static void main(String []avgs) { StringJoiner sj=new StringJoiner(",","[","]"); sj.add("aaa").add("bbb").add("ccc"); System.out.println(sj); System.out.println(sj.length()); } }
转换成字符串:
import java.sql.SQLOutput; import java.util.*; import java.awt.*; public class hello { public static void main(String []avgs) { StringJoiner sj=new StringJoiner(",","[","]"); sj.add("aaa").add("bbb").add("ccc"); System.out.println(sj); System.out.println(sj.length()); String str=sj.toString(); System.out.println(str); } }
6.字符串转换成字符的操作(toCharArray)
基本格式:
s.tocharArray();
代码展示:
import java.sql.SQLOutput; import java.util.*; import java.awt.*; public class hello { public static void main(String []avgs) { Scanner sc = new Scanner(System.in); String str = sc.next(); char []arr=str.toCharArray(); int c=arr[0]-32; System.out.println((char)c); } }
6.综合演练
假如说一个字符串为"abcd",进行旋转一次变成dabc,第二次cdab…
判断是否这两个字符串是否会在n此旋转后回想等,如果相等,那么就返回true,否则返回else.
import java.sql.SQLOutput; import java.util.*; import java.awt.*; public class hello { public static void main(String []avgs) { Scanner sc=new Scanner(System.in); String str=sc.next(); System.out.println(JudgeEqual(str)); } public static boolean JudgeEqual(String s){ //判断是否相等 StringBuilder sb=new StringBuilder(); sb.append(s); sb.reverse(); String st=sb.toString(); for(int i=0;i<s.length();i++){ String str=reverse(s); if(str.equals(st)){ return true; } } return false; } public static String reverse(String s){ //进行截取字符 String start=s.substring(0,1); String end=s.substring(1); String str=end+start; return str; } }