C# 如何修剪 StringBuilder 的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11094600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trim StringBuilder's string?
提问by Pramod Kumar
How can we trim a StringBuildervalue without the overhead caused by using StringBuilder.toString().trim()and thereby creating a new Stringand/or a new StringBuilderinstance for each trim call?
我们如何在StringBuilder没有因使用StringBuilder.toString().trim()而产生的开销的情况下修剪一个值,从而为每个修剪调用创建一个新的String和/或一个新的StringBuilder实例?
采纳答案by Stephen C
Why does StringBuilder don't have trim() method
为什么 StringBuilder 没有 trim() 方法
- Because that's the way it was designed. Try asking the designers1.
- Because there is not much call for it.
- Because the String
trim()semantics and signature is a poor fit for mutable strings, though that is debatable.
- 因为这就是它的设计方式。尝试询问设计师1。
- 因为没有太多的要求。
- 因为字符串
trim()语义和签名不适合可变字符串,尽管这是有争议的。
Either way, the answer is not relevant to solving your problem.
无论哪种方式,答案都与解决您的问题无关。
and how can we trim a StringBuilder value?
我们如何修剪 StringBuilder 值?
The simplest way is to use StringBuilder.toString().trim()...
最简单的方法是使用StringBuilder.toString().trim()...
I don't want to use StringBuilder.toString().trim().
我不想使用 StringBuilder.toString().trim()。
In that case, so you need to do what trim()does under the covers: match and remove the leading and trailing white-space. Since the StringBuilderAPI has no regex support, you'll need to do this that hard way; i.e. by iterating the characters from the front forward and end backward to see what characters need to be removed, etcetera.
在这种情况下,您需要在幕后做些什么trim():匹配并删除前导和尾随空格。由于StringBuilderAPI 没有正则表达式支持,因此您需要以这种方式进行操作;即通过从前向前和向后迭代字符以查看需要删除哪些字符,等等。
Are you sure you wouldn't prefer to do it the easy way? If not, this Q&A has some example implementations, analysis, benchmarking, etcetera:
你确定你不想用简单的方法来做吗?如果没有,此问答有一些示例实现、分析、基准测试等:
Finally, you could implement your own variation of the StringBuilderclass that does have a trim()method. You could possibly use a different internal representation so that operations that remove characters at the start don't copy characters. (I would not recommend this ... but it is an option if you have a pragmatically strong need for trim().)
最后,您可以实现自己的StringBuilder具有trim()方法的类的变体。您可以使用不同的内部表示,以便在开始时删除字符的操作不会复制字符。(我不会推荐这个......但如果你对 . 有务实的强烈需求,这是一个选择trim()。)
Actually i am in a loop where i have to compare this
StringBuilderstring with many other values so if i callStringBuilder.toString().trim()each time, it will create a new instance and i don't want to create a new String object each time.
实际上我在一个循环中,我必须将这个
StringBuilder字符串与许多其他值进行比较,所以如果我StringBuilder.toString().trim()每次调用,它都会创建一个新实例,我不想每次都创建一个新的 String 对象。
The flip-side is that removing characters from the start of a StringBuilderentails copying all of the remaining characters.
另一方面是从 a 的开头删除字符StringBuilder需要复制所有剩余的字符。
Maybe you would be better off turning the complete StringBuilderinto a Stringto start with, then when you use trim()and substring()and the like, you won't be copying characters2.
也许你最好把完整的StringBuilder变成 aString开始,然后当你使用trim()等等时substring(),你就不会复制字符2。
1 - To the people who claim it is "not constructive" to say this, the alternative is to pretend that we werein the room when the design decisions were made and we didhear the debate that occurred. But that would be a lie. Frankly, it isconstructive to point out that nobody here knows the answer, and not pretend otherwise. Why? Because a lot of readers will not be aware that the Java design processes at that time were opaque.
1 -为了谁声称这是“没有建设性”说这个人,另一种方法是假装我们是在室内设计时决定作了,我们确实听到发生了争论。但这将是谎言。坦率地说,指出这里没有人知道答案是有建设性的,而不是假装不知道。为什么?因为很多读者不会意识到当时的Java设计过程是不透明的。
2 - Prior to Java 7, these methods work by creating a new String that sharesthe backing array of the original String... so you only end up copying the string's control information. In Java 7 they changed the implementation of trimand substringso that they used a String constructor that copies a subarray of the backing array.
2 - 在 Java 7 之前,这些方法通过创建一个共享原始后备数组的新字符串来工作String……因此您最终只会复制字符串的控制信息。在 Java 7 中,他们更改了trimand的实现,substring因此他们使用 String 构造函数来复制支持数组的子数组。
回答by Akash KC
You can convert StringBuilderinstance to stringwhich later can be trimmed.
您可以将StringBuilder实例转换为string稍后可以修剪的实例。
sb.toString().trim();//sb is stringBuilder Instance.
回答by Harry Joy
Why does StringBuilder don't have trim() method?
为什么 StringBuilder 没有 trim() 方法?
Hmm.. StringBuilderis like String, just a little difference that it can be modified after creation else it is same as string.
嗯..StringBuilder就像String,只是有点不同,它可以在创建后进行修改,否则它与字符串相同。
As stated in above link, The principal operations on a StringBuilder are the append and insert methods. I guess they are proposed to support mutable strings, same as with StringBuffer. And if you can get things done by a small call to some other method and then desired method then whats the matter. Why you want same implementation in multiple classes.
如上面的链接所述,StringBuilder 的主要操作是 append 和 insert 方法。我猜他们被提议支持可变字符串,与StringBuffer. 如果您可以通过对其他方法的小调用然后使用所需的方法来完成任务,那又怎样。为什么要在多个类中实现相同的实现。
How can we trim a StringBuilder value?
我们如何修剪 StringBuilder 值?
As said by others stringBuilder.toString().trim();
正如其他人所说 stringBuilder.toString().trim();
I don't want to use
StringBuilder.toString().trim().
我不想使用
StringBuilder.toString().trim().
Hmm... matter of choice, any specific reason for this?
嗯......选择问题,有什么具体原因吗?
回答by iracigt
Use StringBuilder.toString().trim()but save it in a variable.
使用StringBuilder.toString().trim()但将其保存在变量中。
For example:
例如:
String myTrimmedString = myStringBuilder.toString().trim();
String myTrimmedString = myStringBuilder.toString().trim();
It doesn't have a trim()method because it is very easy and somewhat preferred to use immutable objects. Imagine one thread is trimming the StringBuilderwhile another is appending to it. Imagine the bugs and weirdness that could cause in your code.
它没有trim()方法,因为使用不可变对象非常容易并且在某种程度上更受欢迎。想象一个线程正在修剪StringBuilder另一个线程正在附加到它。想象一下可能在您的代码中导致的错误和怪异。
回答by KidTempo
I don't know what you are trying to achieve, but if you're worried about performance perhaps you could perform the trim as you are appending to the StringBuilder.
我不知道您想要实现什么,但是如果您担心性能,也许您可以在附加到 StringBuilder 时执行修剪。
stringbuilder.append(string.trim())
stringbuilder.append(string.trim())

