注意点:只有返回值才是被截取的小串,所以需要有一个变量去承接它;
自己写的代码:
import java.util.Scanner;
public class practice {public static void main(String[] args) {Scanner input = new Scanner(System.in);String number;while (true) {System.out.println("输入一个手机号");number = input.next();if (number.length() == 11) {break;} else {System.out.println("请重新输入:");}}System.out.println(number);String str= number.substring(3, 7);System.out.println(str);String cr ="";for(int i=0;i<str.length();i++) {char c = str.charAt(i);c ='*';cr=cr+c;}System.out.println(cr);String str1= number.substring(0, 3);String str2= number.substring(7, 11);System.out.println(str1);System.out.println(str2);String cr1="";cr1=str1+cr+str2;System.out.println(cr1);}
}
代码运行结果:
视频代码:
自己写的代码:
public class practice{public static void main(String [] args){String id = "140227199004200039";String year = id.substring(6,10);String month = id.substring(10,12);String day = id.substring(12,14);String sex = id.substring(16,17);int num = Integer.parseInt(sex);System.out.println(year);if(num % 2==0){sex = "女";}else{sex = "男";}System.out.println(month);System.out.println(day);System.out.println("人物信息为:");System.out.println("出生年月日为:" +year+"年"+month+"月"+day+"日");System.out.println("性别为:"+sex);}
}
代码运行结果:
视频代码:
将字符串里面的是数字的字符转换为数字运算,用id.charAt()获取单个字符,然后通过ASC||码表里面对应来运算,
’0‘是48,’9‘是57;57-48=9;
初步代码:
public class practice{public static void main(String[ ] args){String talk = "你玩的真好,不要再玩了,笑死了";String str = talk.replace("笑死了","***"); System.out.println(str);}
}
代码运行结果截图:
升级代码:
public class practice{public static void main(String[ ] args){String talk = "你玩的真好,不要再玩了,笑死了,太搞笑了";String arr[]= {"笑死了","太搞笑了","6"};for(int i=0;i<arr.length;i++){talk = talk.replace(arr[i],"***");}System.out.println(talk);}
}