1

我有一个包含value >00:01:00. 我需要从这个字符串中删除“ > ”的符号,但我无法做到这一点。

我正在使用以下代码来实现目标,

String duration = "value >00:01:00";
duration.substring(8, duration.length() - 9);
4

6 回答 6

4

你可以做这样的事情

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(">", "");
于 2013-10-29T08:07:09.913 回答
4

你也可以这样做

duration = duration.replace(">", "").trim();
于 2013-10-29T08:10:11.783 回答
2

试试这个:

 String duration = ">00:01:00";
 duration = duration.replace(">","");
 System.out.println(duration);
于 2013-10-29T08:09:03.517 回答
2

您可以通过以下方式进行:

1. public String substring(int beginIndex, int endIndex)

String duration = ">00:01:00";
duration = duration.substring(duration.indexOf('>') + 1, duration.length());

2. public String replace(CharSequence target, CharSequence replacement)

String duration = ">00:01:00";
duration = duration.replace('>', '');

3. 使用 public String substring(int beginIndex)

String duration = ">00:01:00";
duration = duration.substring(duration.indexOf('>') + 1);

4. 使用 public String replaceFirst(String regex, String replacement)

String duration = ">00:01:00";
duration = duration.replaceFirst(">", "");

5. 使用 public String replaceAll(String regex, String replacement)

String duration = ">00:01:00";
duration = duration.replaceAll(">", "");

输出

00:01:00
于 2013-10-29T08:09:58.450 回答
1

duration .substring(duration.length() - 8);应删除 > 符号。

于 2013-10-29T08:09:18.893 回答
0
 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);
  }
于 2013-10-29T08:33:39.413 回答