java:如何检查 StringBuilder 字符以查看它是否包含与来自数组的新字符串请求相同的字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3202861/
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
java: How to check StringBuilder charcters to see if it contains same characters as new string request from array?
提问by user386537
I want to check if a Java StringBuilder
object contains a character.
我想检查一个 JavaStringBuilder
对象是否包含一个字符。
For example I have a list of foods in an array {apple, pear, orange, peach, cherry} and I want to add a random number of these food types to the StringBuilder.
例如,我在数组 {apple、pear、orange、peach、cherry} 中有一个食物列表,我想将这些食物类型的随机数添加到 StringBuilder。
How would I check if StringBuilder contains a character?
我如何检查 StringBuilder 是否包含一个字符?
回答by Michael Borgwardt
You shouldn't be "checking" anything to do that, you should do it so that duplicates never get added in the first place. To do that:
你不应该“检查”任何东西来做到这一点,你应该这样做,这样重复项就不会首先被添加。要做到这一点:
- start with a
List<String>
of the options- copy the list
- add a random element to the result
- remove that element.
- Go back to 3 until you've added as many elements as you want or there aren't anymore left
- 从一个
List<String>
选项开始- 复制清单
- 在结果中添加一个随机元素
- 删除该元素。
- 返回 3,直到您添加了任意数量的元素或不再有剩余元素
回答by Michael Williamson
A different option is to create a List<String>
of all of the options, and use Collections.shuffle
to randomly permute the list. You can then join together the first n elements.
另一个选项是创建List<String>
所有选项中的一个,并用于Collections.shuffle
随机排列列表。然后,您可以将前 n 个元素连接在一起。
Since both Collections.shuffle
and joining together the first n elements takes linear time, the entire process should take linear time.
由于Collections.shuffle
将前 n 个元素连接在一起需要线性时间,因此整个过程应该需要线性时间。
回答by aioobe
import java.util.Random;
public class Test {
public static void main(String args[]) {
String[] fruits = { "apple", "pear", "orange", "peach", "cherry" };
StringBuilder sb = new StringBuilder("Some fruits: apple");
Random r = new Random();
int N = 3;
for (int i = 0; i < N; i++) {
String toAdd;
do {
toAdd = fruits[r.nextInt(fruits.length)];
} while (sb.indexOf(toAdd) != -1);
sb.append(", " + toAdd);
}
System.out.println(sb);
}
}
Output:
输出:
Some fruits: apple, pear, orange, peach
But I agree with Michael Borgwardt on that it's a bad thing to check using index-of over and over again.
但我同意 Michael Borgwardt 的观点,即反复使用 index-of 进行检查是一件坏事。
回答by Marc van Kempen
It is of course possible to check the string by iterating through it (e.g. use indexOf()), but I would use a Set in this particular instance to add the fruits (if it already exists it will not be added again), once you're done adding fruits convert the Set into a String, e.g.
当然可以通过遍历它来检查字符串(例如使用 indexOf()),但是我会在这个特定实例中使用 Set 来添加水果(如果它已经存在,它将不会再次添加),一旦你'完成添加水果将集合转换为字符串,例如
Set<String> fruits = new HashSet<String>();
for (String fruit: fruitSource) {
fruits.add(fruit);
}
StringBuilder sb = new StringBuilder();
for (String fruit: fruits) {
sb.append(fruit);
sb.append(", ");
}
return sb.toString();
回答by Whitecat
A simple way to do it is similar to Marc van Kempen's solution but smaller.
一种简单的方法类似于Marc van Kempen的解决方案,但更小。
Set<String> fruits = new HashSet<String>();
StringBuilder sb = new StringBuilder();
for (String fruit: fruits) {
if(fruits.add(fruit)){
sb.append(fruit);
sb.append(", ");
}
}
return sb.toString();
回答by swapnil bhosale
The StringBuilder
class does not have a contains()
method, but String
does. So you can use StringBuilder
to build a string by using toString()
. Then call contains()
on it, to check if it contains that character or string.
该StringBuilder
班没有一个contains()
方法,但String
确实。因此,您可以通过使用StringBuilder
来构建字符串toString()
。然后调用contains()
它,检查它是否包含该字符或字符串。
Example:
例子:
stringBuilder.toString().contains(characterYouWantToCheck)
回答by Simon Baars
The StringBuilder
class does not have a contains()
method, but it does have an indexOf()
method. The indexOf()
method returns -1
when the substring was not found.
该StringBuilder
班没有一个contains()
方法,但它确实有一个indexOf()
方法。当未找到子字符串时,该indexOf()
方法返回-1
。
Example:
例子:
stringBuilder.indexOf(charactersYouWantToCheck)!=-1
回答by Aaditya Kavthekar
strb2.indexOf(String.valueOf(strb1.charAt(i))
strb2
and strb1
are objects of StringBuilder
class. If the letter is present, it will return its index else -1.
strb2
并且strb1
是StringBuilder
类的对象。如果字母存在,它将返回其索引否则-1.
回答by Sai Nikhil
Java StringBuilder doesn't have containsmethod So you can check whether a string or character contains in the StringBuilder as below:
Java StringBuilder 没有contains方法因此您可以检查字符串或字符是否包含在 StringBuilder 中,如下所示:
StingBuilder sb = new StringBuilder("apple, pear, orange, peach, cherry");
if(sb.indexOf("someFruit") == -1) {
//you can insert
} else {
// do something
}