我有一个包含value >00:01:00
. 我需要从这个字符串中删除“ > ”的符号,但我无法做到这一点。
我正在使用以下代码来实现目标,
String duration = "value >00:01:00";
duration.substring(8, duration.length() - 9);
你可以做这样的事情
String duration = ">00:01:00";
duration = duration.substring(duration.indexOf('>') + 1, duration.length()); // substring from index of that char to a specific length(I've used the length as the end index)
duration = duration.substring(duration.indexOf('>') + 1); // substring from index of that char to the end of the string (@DanielBarbarian's suggestion)
从该特定字符的索引中获取子字符串(您需要 +1,因为您需要来自下一个索引的子字符串)到字符串的末尾。
如果您不想这样提取子字符串,也可以替换该特定字符。
String duration = ">00:01:00";
duration = duration.replace(">", "");
你也可以这样做
duration = duration.replace(">", "").trim();
试试这个:
String duration = ">00:01:00";
duration = duration.replace(">","");
System.out.println(duration);
您可以通过以下方式进行:
String duration = ">00:01:00";
duration = duration.substring(duration.indexOf('>') + 1, duration.length());
String duration = ">00:01:00";
duration = duration.replace('>', '');
String duration = ">00:01:00";
duration = duration.substring(duration.indexOf('>') + 1);
String duration = ">00:01:00";
duration = duration.replaceFirst(">", "");
String duration = ">00:01:00";
duration = duration.replaceAll(">", "");
00:01:00
duration .substring(duration.length() - 8);
应删除 > 符号。
String duration = ">00:01:00";
//Method 1
String result = duration.replace(">", "");
System.out.println(result);
//Method 2
String result2 = duration.substring(1);
System.out.println(result2);
//Method 3
/***
* Robust method but requires these imports
* import java.util.regex.Matcher;
* import java.util.regex.Pattern;
*/
Pattern p = Pattern.compile("(\\d{2}:\\d{2}:\\d{2})") ;
Matcher m = p.matcher(duration);
if (m.find())
{
String result3 = m.group(1);
System.out.println(result3);
}