Java 中的修剪字符
如何在 Java 中修剪字符?
例如
String j = “\joe\jill\”.Trim(new char[] {“\”});
j 应该是
“乔\吉尔”
String j = “jack\joe\jill\”.Trim("jack");
j 应该是
“\乔\吉尔\”
等
How can I trim characters in Java?
e.g.
String j = “\joe\jill\”.Trim(new char[] {“\”});
j should be
"joe\jill"
String j = “jack\joe\jill\”.Trim("jack");
j should be
"\joe\jill\"
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
Apache Commons 有一个很棒的StringUtils 类 (org.apache.commons.lang.StringUtils)。在 StringUtils 中有一个 strip(String, String) 方法可以完成您想要的操作。
无论如何,我强烈建议使用 Apache Commons,尤其是 Collections 和 Lang 库。
Apache Commons has a great StringUtils class (org.apache.commons.lang.StringUtils). In
StringUtils
there is astrip(String, String)
method that will do what you want.I highly recommend using Apache Commons anyway, especially the Collections and Lang libraries.
这可以满足您的要求:
$
用于删除字符串末尾的序列。^
用于删除开头的内容。作为替代方案,您可以使用语法:
|
表示“或”。如果您想修剪其他字符,只需调整正则表达式:
This does what you want:
The
$
is used to remove the sequence in the end of string. The^
is used to remove in the beggining.As an alternative, you can use the syntax:
The
|
means "or".In case you want to trim other chars, just adapt the regex:
CharMatcher
– Google Guava过去,我会选择 Colins 的 Apache commons-lang 答案 。但现在 Google 的 guava-libraries 已发布,CharMatcher 类会很好地完成您想要的操作:
CharMatcher 拥有一组非常简单而强大的 API 以及一些预定义的常量使操作变得非常容易。例如:
非常好的东西。
CharMatcher
– Google GuavaIn the past, I'd second Colins’ Apache commons-lang answer. But now that Google’s guava-libraries is released, the CharMatcher class will do what you want quite nicely:
CharMatcher has a very simple and powerful set of APIs as well as some predefined constants which make manipulation very easy. For example:
Very nice stuff.
这是另一个非正则表达式、非超级棒、非超级优化但非常容易理解的非外部库解决方案:
用法:
Here is another non-regexp, non-super-awesome, non-super-optimized, however very easy to understand non-external-lib solution:
Usage:
您可以使用 Apache Commons Lang StringUtils
You could use
removeStart
andremoveEnd
from Apache Commons Lang StringUtils第一个选项手工制作:
打印
joe\jil
joe\jil
Hand made for the first option:
Prints
joe\jil
joe\jil
我的解决方案:
My solution:
似乎没有现成的 java api 可以做到这一点,但你可以编写一个方法来为你做到这一点。
这个链接可能有用
it appears that there is no ready to use java api that makes that but you can write a method to do that for you.
this link might be usefull
编辑:通过答案进行修改,仅替换第一个和最后一个“\”字符。
EDIT: Amended by answer to replace just the first and last '\' character.
我实际上会编写自己的小函数,通过使用普通的旧字符访问来实现
这一点:这与 String.trim() 的行为类似,只是它使用 '\' 而不是空格。
这是一种可行的替代方案,并且实际使用了trim()。 ;)虽然它不是很有效,但它可能会在性能方面击败所有基于正则表达式的方法。
I would actually write my own little function that does the trick by using plain old char access:
This behaves similar to what String.trim() does, only that it works with '\' instead of space.
Here is one alternative that works and actually uses trim(). ;) Althogh it's not very efficient it will probably beat all regexp based approaches performance wise.
我认为没有任何内置函数可以根据传入的字符串进行修剪。这是一个如何执行此操作的小示例。这可能不是最有效的解决方案,但对于大多数情况来说它可能足够快,可以评估并适应您的需求。我建议测试性能并根据需要对定期使用的任何代码片段进行优化。下面,我提供了一些计时信息作为示例。
这个答案假设要修剪的字符是一个字符串。例如,传入“abc”将修剪“abc”,但不会修剪“bbc”或“cba”等。
以下每1000万次运行的一些性能时间。
" mile ".trim();
运行时间为 248 毫秒 作为性能比较的参考实现。trim( "smiles", "s" );< /code> 运行时间为 547 毫秒 - 大约是 java 的
String.trim()
方法的 2 倍。"smiles".replaceAll("s$|^s","");
运行时间为 12,306 毫秒 - 大约是 java 的String.trim()
方法的 48 倍。并使用编译的正则表达式模式
Pattern pattern = Pattern.compile("s$|^s");
pattern.matcher("smiles").replaceAll("");
运行时间为 7,804 毫秒 - 大约是 java 的String.trim()
方法的 31 倍。I don't think there is any built in function to trim based on a passed in string. Here is a small example of how to do this. This is not likely the most efficient solution, but it is probably fast enough for most situations, evaluate and adapt to your needs. I recommend testing performance and optimizing as needed for any code snippet that will be used regularly. Below, I've included some timing information as an example.
This answer assumes that the characters to be trimmed are a string. For example, passing in "abc" will trim out "abc" but not "bbc" or "cba", etc.
Some performance times for running each of the following 10 million times.
" mile ".trim();
runs in 248 ms included as a reference implementation for performance comparisons.trim( "smiles", "s" );
runs in 547 ms - approximately 2 times as long as java'sString.trim()
method."smiles".replaceAll("s$|^s","");
runs in 12,306 ms - approximately 48 times as long as java'sString.trim()
method.And using a compiled regex pattern
Pattern pattern = Pattern.compile("s$|^s");
pattern.matcher("smiles").replaceAll("");
runs in 7,804 ms - approximately 31 times as long as java'sString.trim()
method.我就是这样做的。
我认为这已经是合理范围内尽可能有效的了。它优化了单字符大小写,并避免为每个删除的子序列创建多个子字符串。
请注意,处理了将空字符串传递给修剪的极端情况(其他一些答案将进入无限循环)。
Here's how I would do it.
I think it's about as efficient as it reasonably can be. It optimizes the single character case and avoids creating multiple substrings for each subsequence removed.
Note that the corner case of passing an empty string to trim is handled (some of the other answers would go into an infinite loop).
这是一个 10 年前的问题,但感觉大多数答案都有点复杂,或者不太符合所问的方式。此外,这里获得最多支持的答案没有提供任何示例。这是我制作的一个简单的课程:
https://gist.github.com/Maxdw/d71afd11db2df4f1297ad3722d6392ec
用法:
10 year old question but felt most of the answers were a bit convoluted or didn't quite work the way that was asked. Also the most upvoted answer here didn't provide any examples. Here's a simple class I made:
https://gist.github.com/Maxdw/d71afd11db2df4f1297ad3722d6392ec
Usage: