在Java中,有没有一种方法可以检查以下条件:
“单个字符是否出现在字符串x中”
而不需要使用循环?
在Java中,有没有一种方法可以检查以下条件:
“单个字符是否出现在字符串x中”
而不需要使用循环?
package com;
public class _index {
public static void main(String[] args) {
String s1="be proud to be an indian";
char ch=s1.charAt(s1.indexOf('e'));
int count = 0;
for(int i=0;i<s1.length();i++) {
if(s1.charAt(i)=='e'){
System.out.println("number of E:=="+ch);
count++;
}
}
System.out.println("Total count of E:=="+count);
}
}
for
不再是一个循环了吗? - Mindwin Remember Monica这样使用怎么样;
let text = "Hello world, welcome to the universe.";
let result = text.includes("world");
console.log(result) ....// true
结果将是true
或false
这对我总是有效
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
static String removeOccurences(String a, String b)
{
StringBuilder s2 = new StringBuilder(a);
for(int i=0;i<b.length();i++){
char ch = b.charAt(i);
System.out.println(ch+" first index"+a.indexOf(ch));
int lastind = a.lastIndexOf(ch);
for(int k=new String(s2).indexOf(ch);k > 0;k=new String(s2).indexOf(ch)){
if(s2.charAt(k) == ch){
s2.deleteCharAt(k);
System.out.println("val of s2 : "+s2.toString());
}
}
}
System.out.println(s1.toString());
return (s1.toString());
}
public int indexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= max) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
final char[] value = this.value;
for (int i = fromIndex; i < max; i++) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
return indexOfSupplementary(ch, fromIndex);
}
}
你可以看到它使用了 for 循环来查找字符。请注意,你在代码中使用的每个 indexOf 都相当于一个循环。
因此,对于单个字符来说不可避免地要使用循环。
然而,如果你想要查找更多形式的特殊字符串,可以使用有用的库,例如 util.regex
,它采用更强大的算法使用 正则表达式 来匹配字符或字符串模式。例如,要在字符串中查找电子邮件:
String regex = "^(.+)@(.+)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
如果你不喜欢使用正则表达式,只需使用循环和 charAt
并尝试在一个循环中涵盖所有情况。
要注意的是,递归方法比循环更费时,因此不建议使用。
如果你想要检查一个字符是否出现在某个字符串中,至少需要使用循环/递归一次来遍历整个字符串(内置方法如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))。