API:应用程序编程接口。
ctrl+alt+v,自动生成一个变量接收数据
字符串:
注意点
创建string对象两种方式
1.直接赋值
2.构造器来创建
详情看黑马JAVA入门学习笔记7-CSDN博客
常用方法:比较
引用数据类型,比较的是地址值。
boolean equals 要求完全一致
boolean equalslgnoreCase 忽略大小写
遍历字符串:
public char charAt(int index)根据索引返回字符
public int length()返回字符串长度
char类型在进行计算的时候,会自动类型提升为int,查询ascii码
案例:
//键盘录入字符串,统计大写字母小写字母数字的个数
public class studentTest {public static void main(String[] args) {Scanner sc=new Scanner(System.in);String str=sc.next();int smallcount=0;int bigcount=0;int numcount=0;for (int i = 0; i <str.length() ; i++) {char c=str.charAt(i);if(c>='a'&&c<='z')smallcount++;else if(c>='A'&&c<='Z')bigcount++;else if(c>='0'&&c<='9')numcount++;}System.out.println("小写字母:"+smallcount);System.out.println("大写字母:"+bigcount);System.out.println("数字:"+numcount);}}
案例:拼接字符串:
//输入一个数组,将数组拼接成字符串
public class studentTest {public static void main(String[] args) {int[] arr={1,2,3};String result=arrToString(arr);System.out.println(result);}public static String arrToString(int[] arr){if(arr==null)return "";else if(arr.length==0)return "[]";else{String res="[";for (int i = 0; i <arr.length ; i++) {if(i==arr.length-1)res+=arr[i];elseres+=arr[i]+",";}res+="]";return res;}}}
字符串反转
fori从0for循环,forr逆着for循环
//定义一个方法,实现字符串反转
public class studentTest {public static void main(String[] args) {String result=reverse("abc");System.out.println(result);}public static String reverse(String s){String res="";for (int i = s.length()-1; i >=0 ; i--) {char c=s.charAt(i);res+=c;}return res;}}
案例:金额转换
数字变成大写中文,查表法,跟数组一一对应
//金额转换限制在七位
public class studentTest {public static void main(String[] args) {Scanner sc=new Scanner(System.in);int money=0;String res="";//转化成大写字母的数字while(true){System.out.println("输入一个金额");money=sc.nextInt();if(money>=0&&money<=9999999){while(true){int a=money%10;String s=changeTocapital(a);res=s+res;money=money/10;if(money==0){break;}}break;}else{System.out.println("金额不对");}}int count=7-res.length();String res1="";for (int i = 0; i <count ; i++) {res1+="零";}res1+=res;String[] arr1={"佰","拾","万","仟","佰","拾","元"};String result="";for (int i = 0; i <res1.length() ; i++) {char c=res1.charAt(i);result=result+c+arr1[i];}System.out.println(result);}//定义一个方法,把数字转换成大写汉字public static String changeTocapital(int num){String[] arr={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};String s=arr[num];return s;}}
案例:手机号屏蔽
string substring(int beginIndex,int endIndex)截取 包头不包尾,包左不包右。只有返回值才是截取的小串。
string substring(int beginIndex)截取到末尾
//屏蔽手机号
public class studentTest {public static void main(String[] args) {String Telenumber="13112340596";String start=Telenumber.substring(0,3);String end=Telenumber.substring(7);String result=start+"****"+end;System.out.println(result);}}
案例 身份信息查看
1、2位:省份;3、4位:城市;56:区县;7-14:出生年月日;1516:所在地派出所;17:性别;18:个人信息码,随机。
//身份证查看
public class studentTest {public static void main(String[] args) {String id="312168202404051129";String year=id.substring(6,10);String month=id.substring(10,12);String day=id.substring(12,14);System.out.println("出生日期是:"+year+"年"+month+"月"+day+"日");char gender=id.charAt(16);//System.out.println('0'+0);//48int g=gender-48;if(g%2==0){System.out.println("性别是女");}else{System.out.println("男");}}}
案例:敏感词替换
string replace(旧值,新值)替换,需要返回值
//敏感词替换
public class studentTest {public static void main(String[] args) {String talk="你玩的真好,tmd,sb";String[] arr={"tmd","sb","cnm","sc"};//敏感词库for (int i = 0; i <arr.length ; i++) {talk=talk.replace(arr[i],"***");}System.out.println(talk);}}
//你玩的真好,***,***
stringBuilder
可以看做容器,创建之后里面内容可变,提高字符串操作效率
构造: public StringBuilder()创建空白的对象。public StringBuilder(string str)里面是有对象
常用方法: append(任意类型)添加数据,reverse()反转容器内容;length()返回长度; tostring()转换成字符串
使用场景:字符串拼接,字符串反转
//stringbuilder
public class studentTest {public static void main(String[] args) {StringBuilder sb=new StringBuilder();sb.append(2.3);sb.append(4);sb.append(true);System.out.println(sb);//打印的是属性值,而不是地址值。sb.reverse();System.out.println(sb);int len=sb.length();System.out.println(len);String str=sb.toString();System.out.println(str);}}
链式 编程
定义一个键盘录入字符串的方法getstring()
在main函数里面写int len=getstring().replace("a","q").substring(1).length()
stringbuilder:sb.append("aaa").append("bbb").append("ccc");
//判断键盘录入字符串是不是对称的,链式编程
public class studentTest {public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.println("输入一个字符串");String str=sc.next();String s=new StringBuilder(str).reverse().toString();if(s.equals(str)){System.out.println("是对称的");}else{System.out.println("不是对称的");}}}
案例 拼接字符串
//拼接字符串
public class studentTest {public static void main(String[] args) {int[] arr={1,2,3};String res=arrToString(arr);System.out.println(res);}public static String arrToString(int[] arr){StringBuilder sb=new StringBuilder("[");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;}}
stringJoiner
可以看做是容器,创建之后里面的内容可变。提高字符串效率,代码编写简洁。
构造方法:public StringJoiner(间隔符号) ; public StringJoiner(间隔符号,开始符号,结束符号)
成员方法: add() length ; tostring
//stringJoiner
public class studentTest {public static void main(String[] args) {StringJoiner sj=new StringJoiner(", ","[","]");sj.add("aaa").add("bbb").add("ccc");System.out.println(sj);int len=sj.length();System.out.println(len);String s=sj.toString();System.out.println(s);}
//[aaa, bbb, ccc]
//15
//[aaa, bbb, ccc]}
s tringbuilder:默认容量16,不够扩容16*2+2,还不够,则以实际长度创建容量。
案例:
//键盘录入一个字符串,长度小于等于9,必须是数字,输出为罗马数字
public class studentTest {public static void main(String[] args) {Scanner sc=new Scanner(System.in);String str="";while (true) {System.out.println("输入一个字符串");str=sc.next();boolean flag=checkStr(str);if(flag){break;}else{System.out.println("重新输入");}}StringBuilder sb=new StringBuilder();for (int i = 0; i <str.length() ; i++) {char c=str.charAt(i);sb.append(changeToLuoma(c-48));}System.out.println(sb);}public static boolean checkStr(String str){if(str.length()>9){return false;}for (int i = 0; i <str.length() ; i++) {char c=str.charAt(i);if(c<'0'||c>'9'){return false;}}return true;}public static String changeToLuoma(int index){String[] luoma={"","I","II","III","IV","V","VI","VII","VIII","IX"};return luoma[index];}}
另一种方法:使用switch
//键盘录入一个字符串,长度小于等于9,必须是数字,输出为罗马数字
public class studentTest {public static void main(String[] args) {Scanner sc=new Scanner(System.in);String str="";while (true) {System.out.println("输入一个字符串");str=sc.next();boolean flag=checkStr(str);if(flag){break;}else{System.out.println("重新输入");}}StringBuilder sb=new StringBuilder();for (int i = 0; i <str.length() ; i++) {char c=str.charAt(i);sb.append(changeToLuoma(c));}System.out.println(sb);}public static boolean checkStr(String str){if(str.length()>9){return false;}for (int i = 0; i <str.length() ; i++) {char c=str.charAt(i);if(c<'0'||c>'9'){return false;}}return true;}public static String changeToLuoma(char number){String str;switch(number){case '0':str="";break;case '1':str="I";break;case '2':str="II";break;case '3':str="III";break;case '4':str="IV";break;case '5':str="V";break;case '6':str="VI";break;case '7':str="VII";break;case '8':str="VIII";break;case '9':str="IX";break;default:str="";break;}return str;}}
案例:旋转字符串
修改字符串,两个方法:用substring截取,拼接。把字符串先变成字符数组,调整里面的数据,然后变成字符串。
//定义两个字符串,旋转字符串a,若干次之后是否和b一样,旋转是将第一个字符放在最后一位
public class studentTest {public static void main(String[] args) {String str1="abcde";String str2="cdeab";boolean res=check(str1,str2);System.out.println(res);}//旋转方法 使用substringpublic static String rotate(String str){char first=str.charAt(0);String end =str.substring(1);String res=end+first;return res;}//判断方法,旋转若干次,是不是一样两个字符串public static boolean check(String str1,String str2){for (int i = 0; i <str1.length() ; i++) {str1=rotate(str1);if(str1.equals(str2)){return true;}}return false;}}
转为字符数组 tochararray
//定义两个字符串,旋转字符串a,若干次之后是否和b一样,旋转是将第一个字符放在最后一位
public class studentTest {public static void main(String[] args) {String str1="abcde";String str2="cdeab";boolean res=check(str1,str2);System.out.println(res);}//旋转方法 先转换为字符数组,public static String rotate(String str){char[] newarr=str.toCharArray();char c=newarr[0];for (int i = 1; i <newarr.length ; i++) {newarr[i-1]=newarr[i];}newarr[newarr.length-1]=c;String result=new String(newarr);return result;}//判断方法,旋转若干次,是不是一样两个字符串public static boolean check(String str1,String str2){for (int i = 0; i <str1.length() ; i++) {str1=rotate(str1);if(str1.equals(str2)){return true;}}return false;}}
案例:打乱字符串
//键盘输入任意字符串,打乱顺序输出
public class studentTest {public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.println("输入一个任意字符串");String str= sc.next();char[] arr=str.toCharArray();Random r=new Random();for (int i = 0; i <arr.length ; i++) {int index=r.nextInt(arr.length);char temp=arr[i];arr[i]=arr[index];arr[index]=temp;}String res=new String(arr);System.out.println(res);}}
案例:产生验证码,五位,一位数字
//生成验证码 四位字母一位数字
public class studentTest {public static void main(String[] args) {String data="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";Random r=new Random();String str="";for (int i = 0; i <4 ; i++) {int index=r.nextInt(data.length());str+=data.charAt(index);}str+=r.nextInt(10);int num=r.nextInt(5);char[] arr=str.toCharArray();char temp=arr[4];arr[4]=arr[num];arr[num]=temp;String res=new String(arr);System.out.println(res);}}
案例 两个字符串形式的数相乘,结果还是字符串
//给定两个字符串形式非负整数,求他们相乘之后的值,乘积也用字符串表示
public class studentTest {public static void main(String[] args) {String str1="123456";String str2="5221";int num1=stringToInt(str1);int num2=stringToInt(str2);int res= num1*num2;String result="";result+=res;System.out.println(result);}public static int stringToInt(String str){int num=0;for (int i = 0; i <str.length()-1 ; i++) {char c= str.charAt(i);num+=(c-48);num=num*10;}num+=str.charAt(str.length()-1)-48;return num;}}
案例 求最后一个单词长度
//给你一个字符串,由若干单词组成,单词之间是空格隔开,求最后一个单词的长度
public class studentTest {public static void main(String[] args) {String s="hello i am a girl";int count=0;for (int i = s.length()-1; i >=0 ; i--) {if(s.charAt(i)!=' '){count++;}elsebreak;}System.out.println(count);}}