Home > Back-end >  Determine whether A of the strings contain another string B, the string can be A discontinuity
Determine whether A of the strings contain another string B, the string can be A discontinuity

Time:09-23

If the string A: abcdefgh, string B: abcdg can also be A substring, this kind of circumstance how to judge, in c + + implementation

CodePudding user response:

 void __fastcall TForm1: : Button1Click (TObject * Sender) 
{
AnsiString a, b;
A="Hello, world".
B="wor";//"word"

Int Pos=Amy polumbo OS (b);//in order to compatible with Delphi, the subscript starting from 1
ShowMessage (IntToStr (Pos));
If (Pos> 0)
ShowMessage (a + "contains" + b);
The else
ShowMessage (a + "doesn 't contain" + b);

Char * sub=STRSTR (a.c _str (), the biggest _str ());
ShowMessage (AnsiString (sub));
If (sub)
ShowMessage (a + "contains" + b);
The else
ShowMessage (a + "doesn 't contain" + b);
}

CodePudding user response:

"If the string A: abcdefgh, string B: abcdg can also be A substring" this shows that as long as B in each character in A are ok, you can use set operations,

CodePudding user response:

Don't understand, changed or substring abcd order?

CodePudding user response:

According to his meaning is abcdg abcdefgh substring, regardless of the order is set operations, if consider order, need each character match from the front to rear, first find a, if found, from the next position to go backwards b...

CodePudding user response:

Upstairs positive solution, regardless of the order of words, need one by comparing
 
Void __fastcall TForm1: : Button1Click (TObject * Sender)
{
AnsiString a, b, STR;
A="Hello, world".
B="wor";//"word"
STR=a + "contains" a + b;
for(int i=0; i{
Int t=Amy polumbo OS (b.S ubString (I + 1, 1));
If (t<=0)
{
STR=a + "doesn 't contain" a + b;
Break;
}
}
ShowMessage (STR);
}
  • Related