【问题标题】:Java replace a character in a string between two characters using the indexes from the substring functionJava 使用 substring 函数中的索引替换两个字符之间的字符串中的一个字符
【发布时间】:2018-11-02 10:19:55
【问题描述】:

我有一个这样的字符串日期:10-10-2018,我想显示为 2018 年 10 月 10 日,我正在执行以下操作:

String date = "10-10-2018";
String newDate = date.replace(date.subString(date.indexOf("-")+1,date.lastIndexOf("-")),"October");

我没有得到 2018 年 10 月 10 日,而是得到了 2018 年 10 月 10 日,尽管我选择了“-”符号之间的中间一个,但为什么两个 10 的实例都被替换了。我错过了什么。

【问题讨论】:

  • date.subString(date.indexOf("-")+1,date.lastIndexOf("-")) 返回“10”。看看使用 String.split 它将为您提供 3 个部分。将中间部分替换为字符串数组String [] months = {"Jan", "Feb", "Mar"}中的对应索引,然后重构字符串。

标签: java replace substring


【解决方案1】:

简单的问题是,您将字符串中的字符串"10" 替换为"October",而该字符串中存在相同子字符串的其他实例。

您的电话相当于:

date.replace("10", "October");

这也将替换"10" 的第一个实例。 String.replace 的文档包含以下内容:

用指定的文字替换序列替换此字符串中与文字目标序列匹配的每个子字符串。替换从字符串的开头到结尾进行,例如,将字符串“aaa”中的“aa”替换为“b”将得到“ba”而不是“ab”。

更可靠的方法是使用日期/时间 API 来解析和格式化您的日期:

String date = "10-10-2018";
LocalDate d = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
String newDate = d.format(DateTimeFormatter.ofPattern("dd-MMMM-yyyy"));

newDate 按预期计算为"10-October-2018"

【讨论】:

  • 绝对应该使用日期 API。
【解决方案2】:

这就是你需要的:)

public static void main(String[] args) throws ParseException {
    String date = "10-10-2018";

    SimpleDateFormat givenFormat = new SimpleDateFormat("dd-MM-yyyy");  
    SimpleDateFormat newFormat = new SimpleDateFormat("dd-MMM-yyyy");


    Date givenDate=givenFormat.parse(date); 
    System.out.println(givenDate);


    String newDate=newFormat.format(givenDate); 
    System.out.println(newDate);
}

【讨论】:

  • 欢迎 :) 使用 dd-MMMM-yyyy 整月(4 M)
【解决方案3】:
String newDate = date.replace(date.subString(date.indexOf("-")+1,date.lastIndexOf("-")),"October");

让我们打开它。你是含蓄的

int firstHyphen = date.indexOf("-");
int lastHyphen = date.lastIndexOf("-");

但是对于给定的字符串,这和说的一样

int firstHyphen = 2;
int lastHyphen = 5;

接下来,你有一个隐式变量的创建

String month = date.subString(firstHyphen+1, lastHyphen);

相同
String month = date.subString(3, 5);

但是什么子字符串从 3 开始并在 5 之前结束?

String month = "10";

所以我们可以将您的原始代码替换为

String newDate = date.replace("10", "October");

现在应该很清楚,您仅限于连字符之间的字符串部分。该限制仅适用于子字符串。在替换中使用子字符串的结果后,您只需将其传递给一个字符串。特别是,您传递了"10",它在字符串中出现了两次,因此它替换了两次。

如果你想尝试让这个工作,你可以改变你原来的代码看起来像

String newDate = date.replace(date.subString(date.indexOf("-"), date.lastIndexOf("-")), "-October");

这会将"-10" 替换为"-October"。由于"-10" 在字符串中只出现一次,它只会做一次替换。

如果您有一个将整数月份映射到名称的Map,您可以像这样使用它

Map<String, String> months = new HashMap<>();
months.put("10", "October");
String month = date.subString(date.indexOf("-"), date.lastIndexOf("-"));
String newDate = date.replace(month, "-" + months.get(month.subString(1)));

这会即时构建"-October",并将在其他月份工作,假设您使用它们填充地图。但正如其他人所指出的,使用 Java 的内置日期解析和格式化可能更可靠。

【讨论】:

  • 感谢您的解释,最后以下代码有效:String newDate = date.substring(0,date.indexOf("-")+1)+"month name"+date.substring(date.lastIndexOf("-"));
猜你喜欢
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多