简体   繁体   English

替换字符串中的值

[英]replace values in string

I have the following code and in need to replace the value on time from PT14H01M00S to 14:01:00 the following code is omit the first two char but I get 14H01M00S ,there is no way to do that without using string builder? 我有以下代码,需要按时从PT14H01M00S替换14:01:00的值,以下代码省略了前两个字符,但我得到了14H01M00S,如果不使用字符串生成器,有没有办法做到这一点?

String time = "PT14H01M00S";
String substring = time .substring(2);
substring.replace("H", ":");
substring.replace("M", ":");
substring.replace("S", "");

System.out.println(substring);

You could use a regex and replace 您可以使用正则表达式替换

^..(\d\d)H(\d\d)M(\d\d)S$

with

$1:$2:$3

(I leave the details of the code as an exercise for the reader.) (我将代码的细节留给读者练习。)

you forgot to do 你忘了做

String time = "PT14H01M00S";
String substring = time .substring(2);
substring = substring.replace("H", ":");
substring = substring.replace("M", ":");
substring = substring.replace("S", "");

System.out.println(substring);

so the substring is not getting the new value returned from replace method 所以substring没有获得replace方法返回的新值

ran it in here and got 这里跑去

在此处输入图片说明

It seems like you are trying to parse and format a JodaTime Period . 似乎您正在尝试解析和格式化JodaTime Period You should not do it using the String class API. 您不应该使用String类API来执行此操作。 Use appropriate formatter already provided in JodaTime API. 使用JodaTime API中已经提供的适当格式器。

You should build 2 PeriodFormatter s using PeriodFormatterBuilder class, one for parsing the given string into a Period , and then second for formatting the Period to the required format: 您应该使用PeriodFormatterBuilder类构建2个PeriodFormatter ,一个用于将给定的字符串解析为Period ,然后一个用于将Period格式化为所需格式:

String periodString = "PT14H01M00S";

PeriodFormatter parser = new PeriodFormatterBuilder()
        .appendLiteral("PT")
        .appendHours().appendSuffix("H")
        .appendMinutes().appendSuffix("M")
        .appendSeconds().appendSuffix("S")
        .toFormatter();

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .minimumPrintedDigits(2)
        .printZeroAlways()
        .appendHours()
        .appendSeparator(":")
        .appendMinutes()
        .appendSeparator(":")
        .appendSeconds()
        .toFormatter();

System.out.println(formatter.print(parser.parsePeriod(periodString)));

Output: 输出:

14:01:00

use this code instead : 使用此代码代替:

 String time = "PT14H01M00S";
 String substring = time .substring(2);
 substring = substring.replace("H", ":");
 substring = substring.replace("M", ":");
 substring = substring.replace("S", "");

我认为这应该工作

String time = "PT14H01M00S".substring(2, time.length - 1).replace(/H|M/, ":");

the replace method doesn't change the string it was called upon, it returns a new string. replace方法不会更改被调用的字符串,而是返回一个新字符串。

So what you want is: 所以您想要的是:

String time = "PT14H01M00S";
String substring = time .substring(2);
substring = substring.replace("H", ":");
substring = substring.replace("M", ":");
substring = substring.replace("S", "");

I recommend the use of regular expressions to do this kind of tasks. 我建议使用正则表达式来执行此类任务。 They are very powerful to search and replace patterns of text. 它们在搜索和替换文本模式方面非常强大。

use this code instead 使用此代码

 String substring = time .substring(2);
 substring = substring.replaceAll("[HM]",":").replace("S", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM