在Java中有一种方法来检查条件:
"这个字符是否出现在字符串x中"
不使用循环?
在Java中有一种方法来检查条件:
"这个字符是否出现在字符串x中"
不使用循环?
当前回答
您可以使用String类中的两个方法。
string .contains()检查字符串是否包含指定的char值序列 string . indexof()返回字符串中第一次出现指定字符或子字符串的索引,如果没有找到该字符则返回-1(此方法有4种变体)
方法1:
String myString = "foobar";
if (myString.contains("x") {
// Do something.
}
方法2:
String myString = "foobar";
if (myString.indexOf("x") >= 0 {
// Do something.
}
链接作者:Zach Scrivena
其他回答
如果不使用循环/递归至少检查一次字符串,您将无法检查char是否出现在某些字符串中(像indexOf这样的内置方法也使用循环)
如果不是。如果你在字符串中查找一个字符,x比字符串的长度要多得多,我建议使用Set数据结构,因为这比简单地使用indexOf更有效
String s = "abc";
// Build a set so we can check if character exists in constant time O(1)
Set<Character> set = new HashSet<>();
int len = s.length();
for(int i = 0; i < len; i++) set.add(s.charAt(i));
// Now we can check without the need of a loop
// contains method of set doesn't use a loop unlike string's contains method
set.contains('a') // true
set.contains('z') // false
使用set,你将能够在常数时间O(1)检查字符是否存在于字符串中,但你也将使用额外的内存(空间复杂度将是O(n))。
如果您需要经常检查相同的字符串,您可以预先计算字符出现的次数。这是一个使用位数组包含在长数组中的实现:
public class FastCharacterInStringChecker implements Serializable {
private static final long serialVersionUID = 1L;
private final long[] l = new long[1024]; // 65536 / 64 = 1024
public FastCharacterInStringChecker(final String string) {
for (final char c: string.toCharArray()) {
final int index = c >> 6;
final int value = c - (index << 6);
l[index] |= 1L << value;
}
}
public boolean contains(final char c) {
final int index = c >> 6; // c / 64
final int value = c - (index << 6); // c - (index * 64)
return (l[index] & (1L << value)) != 0;
}}
you can use this code. It will check the char is present or not. If it is present then the return value is >= 0 otherwise it's -1. Here I am printing alphabets that is not present in the input.
import java.util.Scanner;
public class Test {
public static void letters()
{
System.out.println("Enter input char");
Scanner sc = new Scanner(System.in);
String input = sc.next();
System.out.println("Output : ");
for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
if(input.toUpperCase().indexOf(alphabet) < 0)
System.out.print(alphabet + " ");
}
}
public static void main(String[] args) {
letters();
}
}
//Ouput Example
Enter input char
nandu
Output :
B C E F G H I J K L M O P Q R S T V W X Y Z
我不确定最初的海报到底想要什么。由于indexOf(…)和contains(…)都可能在内部使用循环,也许他想看看如果没有循环这是否可行?我可以想到两种方法,一种当然是递归
public boolean containsChar(String s, char search) {
if (s.length() == 0)
return false;
else
return s.charAt(0) == search || containsChar(s.substring(1), search);
}
另一个远没有那么优雅,但完整…:
/**
* Works for strings of up to 5 characters
*/
public boolean containsChar(String s, char search) {
if (s.length() > 5) throw IllegalArgumentException();
try {
if (s.charAt(0) == search) return true;
if (s.charAt(1) == search) return true;
if (s.charAt(2) == search) return true;
if (s.charAt(3) == search) return true;
if (s.charAt(4) == search) return true;
} catch (IndexOutOfBoundsException e) {
// this should never happen...
return false;
}
return false;
}
当然,行数会随着您需要支持越来越长的字符串而增长。但是根本没有循环/递归。如果您担心length()使用循环,您甚至可以删除长度检查。
要检查字符串中是否存在某些东西,您至少需要查看字符串中的每个字符。所以即使你没有显式地使用循环,它也会有同样的效率。也就是说,您可以尝试使用str.contains(""+char)。