English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The CASE_INSENSITIVE field of the Pattern class matches characters regardless of case. When this value is used ascompile()
method flag value, and if using regular expression search characters, characters in both cases will match.
Note-By default, this flag only matches ASCII characters
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CASE_INSENSITIVE_Example { public static void main( String args[] ) { Scanner sc = new Scanner(System.in); System.out.println("Enter input data:"); String input = sc.nextLine(); System.out.println("Enter required character:"); char ch = sc.next().toCharArray()[0]; //Regular expression to find the required character String regex = "["+ch+"]"; //Regular expression compilation //compile regular expression //search matcher object Matcher matcher = pattern.matcher(input); int count = 0; while (matcher.find()) { count++; } System.out.println("The letter "+ch+" occurred "+count+" times in the given text (irrespective of case)"); } }
output result
Enter input data: oldtoolbag.com originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. Enter required character: T Буква T встречается 20 раз в данном тексте (независимо от регистра)
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class VerifyBoolean { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Введите строковое значение: "); String str = sc.next(); Pattern pattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); if(matcher.matches()){ System.out.println("Даная строка является типом boolean"); } else { System.out.println("Даная строка не является типом boolean"); } } }
Введите строковое значение: истина Даная строка является типом boolean
Введите строковое значение: ложно Даная строка является типом boolean
Введите строковое значение: hello Даная строка не является типом boolean