Java 文本是否包含子字符串
如何检查某个字符串是否包含像“ABC72961”这样的特定字符串。因此,我们搜索以“ABC”开头,后跟5位数字的字符串。我已经实现了一个算法,但我希望它与“匹配”或其他方式匹配,然后检查这两个解决方案的速度。您可以像这样使用String indexOf命令:Java 文本是否包含子字符串,java,algorithm,pattern-matching,substring,string-matching,regex,Java,Algorithm,Pattern Matching,Substring,String Matching,Regex,如何检查某个字符串是否包含像“ABC72961”这样的特定字符串。因此,我们搜索以“ABC”开头,后跟5位数字的字符串。我已经实现了一个算法,但我希望它与“匹配”或其他方式匹配,然后检查这两个解决方案的速度。您可以像这样使用String indexOf命令: int result = someString.indexOf("ABC72961") 如果没有匹配项,结果将为-1 如果存在匹配,结果将是匹配开始的索引 str.contains("ABC72961"); 如果str包含字符串,则返回
int result = someString.indexOf("ABC72961")
如果没有匹配项,结果将为-1
如果存在匹配,结果将是匹配开始的索引
str.contains("ABC72961");
如果
str
包含字符串,则返回true。如果不是,则为False。我认为您想要使用的是java.util.regex.Pattern
Pattern p = Pattern.compile("ABC(\d*)");
Matcher m = p.matcher("ABC72961");
boolean b = m.matches();
或者,如果“ABC”后面正好是5位数字,则可以使用regexABC(\d{5})
另一个解决办法是:
String stringToTest = "ABC72961"
boolean b = stringToTest.contains("ABC");
您可能需要为此使用
^ABC[0-9]{5}$
:字符串的开头^
:逐字匹配ABC(区分大小写)ABC
:匹配0到9之间的5个数字[0-9]{5}
:字符串结尾$
String#matches
对其进行测试
例子
编辑 看到您的评论,您希望它也适用于
String one=“textABC72961text”
。要做到这一点,您只需删除限制字符串的^
和$
*ABC[0-9]{5}.*
编辑2 如果你想提取它,这里是
if (s.matches(".*ABC[0-9]{5}.*")) {
Matcher m = Pattern.compile("ABC[0-9]{5}").matcher(s);
m.find();
result = m.group();
}
公共字符串getString(){
String str=extractString();
返回str;
}
公共布尔存在(){
return!getString().trim().equals(“”)?false:true;
}
私有列表getPositionsOfABC(){
列表位置=新的ArrayList();
int index=text.indexOf(“ABC”);
而(索引>0){
位置。添加(索引);
索引=text.indexOf(“ABC”,索引+1);
}
返回位置;
}
私有静态布尔isInteger(字符串str){
布尔值isValidInteger=false;
试一试{
整数(str);
isValidInteger=true;
}捕获(NumberFormatException ex){
返回isValidInteger;
}
返回isValidInteger;
}
私有字符串提取字符串(){
List positions=getPositionsOfABC();
用于(整数位置:位置){
int index=position.intValue();
String substring=text.substring(索引,索引+数字长度);
字符串lastDigits=substring.substring(3,substring.length());
if(isInteger(最后位数)){
返回子串;
}
}
返回“”;
}
下面是一个简单的代码,它检查字符串中是否存在子字符串,而不使用库函数、正则表达式或其他复杂的数据结构
class SSC {
public static void main(String[] args) {
String main_str <-- MAIN STRING
String sub_str <-- SUBSTRING
String w; int flag=0;
for(int i=0;i<=main_str.length()-sub_str.length();i++){
w="";
for(int j=0;j<sub_str.length();j++){
w+=main_str.charAt(i+j);
}
if(w.equals(sub_str))
flag++;
}
if(flag>0)
System.out.print("exists "+flag+" times");
else
System.out.print("doesn't exist");
}
}
类SSC{
公共静态void main(字符串[]args){
字符串main_str在将来确保您用适当的语言标记您的问题。我忘了。Tack so Mike:)这听起来像一个正则表达式解决方案。是的,您是对的。但是如何为此编写正则表达式。可能是重复的,但字符串始终包含“ABC”但是五位数字可能不同。我要说的是,是否有一个字符串以ABC开头,后跟5位数字。str.contains(“ABC”);
我必须从给定的文本返回此字符串,或者在给定的文本中查找此类字符串。好的,现在每次有人提出建议时,您只是在更改所需的结果。不,我不想这样做。有人可以更正我的问题。例如,我有String one=“textABC72961text”,我需要返回“ABC72961”因为它与模式匹配。是的,但字符串始终包含“ABC”,但五位数字可能不同。我必须指出是否有一个字符串以ABC开头,后跟5位数字。是的,但您的解决方案不适用于字符串1=“textABC72961text”;如果该字符串存在,我需要返回它。@myanmar如果该字符串适用于此,则下次您需要排除^
和$
@myanmar。请确保您的指定并发布它们。这将使每个人都能轻松找到正确答案;您完全正确。但它不再有效。因此,我得到了错误的结果。@YassinHajaj使用\\w
意味着它只在所有其他字符都是单词字符但也允许使用特殊字符时才查找子字符串,而使用+
意味着必须有这样的字符。替换为*
,如匹配(.*ABC[0-9]{5}.*)
。
public String getString() {
String str = extractString();
return str;
}
public boolean exists() {
return !getString().trim().equals("") ? false : true;
}
private List<Integer> getPositionsOfABC() {
List<Integer> positions = new ArrayList<>();
int index = text.indexOf("ABC");
while (index > 0) {
positions.add(index);
index = text.indexOf("ABC", index + 1);
}
return positions;
}
private static boolean isInteger(String str) {
boolean isValidInteger = false;
try {
Integer.parseInteger(str);
isValidInteger = true;
} catch (NumberFormatException ex) {
return isValidInteger;
}
return isValidInteger;
}
private String extractString() {
List<Integer> positions = getPositionsOfABC();
for (Integer position : positions) {
int index = position.intValue();
String substring = text.substring(index, index + LENGTH_OF_DIGITS);
String lastDigits = substring.substring(3, substring.length());
if (isInteger(lastDigits)) {
return substring;
}
}
return "";
}
class SSC {
public static void main(String[] args) {
String main_str <-- MAIN STRING
String sub_str <-- SUBSTRING
String w; int flag=0;
for(int i=0;i<=main_str.length()-sub_str.length();i++){
w="";
for(int j=0;j<sub_str.length();j++){
w+=main_str.charAt(i+j);
}
if(w.equals(sub_str))
flag++;
}
if(flag>0)
System.out.print("exists "+flag+" times");
else
System.out.print("doesn't exist");
}
}