如何在java字符串中的特定位置使用特定字符
我有一个java字符串,其中包含如何在java字符串中的特定位置使用特定字符,java,string,Java,String,我有一个java字符串,其中包含值>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('>')
值>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)
duration = duration.replace(">", "").trim();
从该特定字符的索引(需要+1,就像需要下一个索引中的子字符串一样)到字符串末尾获取子字符串
如果不想提取这样的子字符串,也可以替换该特定字符
String duration = ">00:01:00";
duration = duration.replace(">", "");
你可以这样做
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)
duration = duration.replace(">", "").trim();
从该特定字符的索引(需要+1,就像需要下一个索引中的子字符串一样)到字符串末尾获取子字符串
如果不想提取这样的子字符串,也可以替换该特定字符
String duration = ">00:01:00";
duration = duration.replace(">", "");
试试这个:
String duration = ">00:01:00";
duration = duration.replace(">","");
System.out.println(duration);
试试这个:
String duration = ">00:01:00";
duration = duration.replace(">","");
System.out.println(duration);
duration.substring(duration.length()-8)代码>应删除>符号。duration.substring(duration.length()-8)代码>应删除>符号。您可以通过以下方式执行:
1.公共字符串子字符串(int beginIndex、int endIndex)
2.公共字符串替换(CharSequence目标,CharSequence替换)
3.使用公共字符串子字符串(int beginIndex)
4.使用公共字符串replaceFirst(字符串正则表达式、字符串替换)
5.使用公共字符串replaceAll(字符串正则表达式、字符串替换)
输出
您可以通过以下方式进行操作:
1.公共字符串子字符串(int beginIndex、int endIndex)
2.公共字符串替换(CharSequence目标,CharSequence替换)
3.使用公共字符串子字符串(int beginIndex)
4.使用公共字符串replaceFirst(字符串正则表达式、字符串替换)
5.使用公共字符串replaceAll(字符串正则表达式、字符串替换)
输出
你也可以这样做
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)
duration = duration.replace(">", "").trim();
你也可以这样做
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)
duration = duration.replace(">", "").trim();
您的解决方案是正确的,但如果您只想从起始索引中获取字符串的其余部分,则可以使用duration.substring(duration.indexOf('>')+1)@DanielBarbarian-Edited。谢谢你的建议!:)您的解决方案将出现编译错误,因为如果看到replace方法,它将char作为参数而不是字符串:你说得对,兄弟。我先写了,但后来看到docs but char参数占用了空间,而不是“”,谢谢:)您的解决方案是正确的,但是如果您只想从起始索引中获取字符串的其余部分,则可以使用duration.substring(duration.indexOf('>')+1)@danielbarbararian-Edited。谢谢你的建议!:)您的解决方案将出现编译错误,因为如果看到replace方法,它将char作为参数而不是字符串:你说得对,兄弟。我先写了这个,但后来看到docs但char参数占用空间而不是“”,谢谢:)与R.J的解决方案相同:您的解决方案是正确的,但如果您只需要起始索引中的字符串的其余部分,则可以使用duration.substring(duration.indexOf('>')+1)有什么叫做空字符吗?!这里与R.J的解决方案相同:您的解决方案是正确的,但如果您只想从起始索引中获取字符串的其余部分,则可以使用duration.substring(duration.indexOf('>'))+1)是否有所谓的空字符?!在给定的示例中,duration.length()是9,因此返回的是duration.substring(0),它是未更改的原始字符串。确定这是复制粘贴错误。我考虑的字符串是“>00:01:00.”在给定的示例中,duration.length()是9,因此返回的是duration.substring(0),它是未更改的原始字符串。确定这是复制粘贴错误。我考虑的字符串是“>00:01:00。”我先写对了,然后改为“”…你是对的,兄弟我先写对了,然后改为“”…你是对的,兄弟