开发者社区 问答 正文

怎样用replaceFirst() 方法来在Java中进行字符串的替换啊?

已解决

怎样用replaceFirst() 方法来在Java中进行字符串的替换啊?

展开
收起
游客3gzok77e677xw 2022-04-03 10:21:20 689 分享 版权
1 条回答
写回答
取消 提交回答
  • 推荐回答
    replaceFirst() 方法用于将目标字符串中匹配某正则表达式的第一个子字符串替换成新的字符串,其语法形式如下:
    字符串.replaceFirst(String regex, String replacement)
    其中,regex 表示正则表达式;replacement 表示用于替换的字符串。例如:
    
    String words = "hello java,hello php";
    String newStr = words.replaceFirst("hello","你好 ");
    System.out.println(newStr);    // 输出:你好 java,hello php
    
    2022-04-03 13:08:12
    赞同 展开评论