• 1.关于String substring有俩个题 2.String replace(“老值“,“新值“)


     

    1. public class benxi {
    2. public static void main(String[] args) {
    3. //获取手机号
    4. String phone="18538371706";
    5. //用方法substring来获取 包头不包尾
    6. String su = phone.substring(0, 3);
    7. //用方法substring获取 一直到末尾的
    8. String s=phone.substring(7);
    9. //拼接一块
    10. String result=su+"****"+s;
    11. System.out.println(result);
    12. }
    13. }

    1.String substring(int beginIndex,int endIndex)

    注意 这个里面放是索引 包头不包尾 包左不包右

    2.String substring(int beginIndex)

    注意 这个截取到末尾

     

    1. public class benxi {
    2. public static void main(String[] args) {
    3. //定义身份证字符串
    4. String sfz="411523200012260412";
    5. //年份
    6. String n=sfz.substring(6,10);
    7. //月份
    8. String y = sfz.substring(10, 12);
    9. //日
    10. String r = sfz.substring(12, 14);
    11. System.out.println("年份"+n+"月份"+y+"日"+r);
    12. //性别 倒数第二位 单数男双数女
    13. char gender=sfz.charAt(16); //直接输出是字符 ‘3’ 需要变成3 '3'为51
    14. int num=gender-48;
    15. if (num%2==0){
    16. System.out.println("性别为女性");
    17. }else{
    18. System.out.println("性别为男性");
    19. }
    20. }
    21. }

    注意 char gender=sfz.charAt(16); 直接输出是索引16的字符‘3’ 要转换为数字3 ASCII表 可以System.out.println(‘0’+0);查询

    0为48 依次增加所以 gender-48

     

    1. public class benxi {
    2. public static void main(String[] args) {
    3. //敏感词替换
    4. //定义说的话
    5. String talk="你玩的真好 tmd cnm";
    6. //定义 当有很多敏感词时用数组来
    7. String arr[]={"tmd","cnm","mlgb","lj"};
    8. //循环敏感词数组中每一个
    9. for (int i = 0; i < arr.length; i++) {
    10. //词替换然后交回talk
    11. talk=talk.replace(arr[i], "***");
    12. }
    13. System.out.println(talk);
    14. }
    15. }

    String replace("老值","新值")

  • 相关阅读:
    KNN-K近邻算法(K-Nearest Neighbors)
    QSS 自定义QLineEdit
    Nacos集群部署遇到问题
    A股风格因子看板 (2023.11 第11期)
    “蔚来杯“2022牛客暑期多校训练营6 A题: Array
    基于ClickHouse解决活动海量数据问题
    Java从入门到精通
    javafx事件总线之EventBus
    C语言日记 37 类的友元(1)(全局函数做友元)
    Android逆向——脱壳解析
  • 原文地址:https://blog.csdn.net/m0_67911983/article/details/128045291