30
String content = "Jane";
String container = 'A.Sven,G.Jane,Jack'; // This is the string which i need to be searched with string content

boolean containerContainsContent = StringUtils.containsIgnoreCase(container, content); // I used to write like this in java

I am new to Delphi. Is there a contains command in Delphi or any other command which performs the same operation?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
delsql
  • 601
  • 2
  • 7
  • 15
  • https://stackoverflow.com/questions/30180634/is-the-contains-delphi-string-helper-case-sensitive if s.ToUpper.Contains(a.ToUpper) then – Akella225 Aug 06 '18 at 07:58

2 Answers2

64

You can use the functions in StrUtils in Delphi

uses
  StrUtils;
..
    if ContainsText('A.Sven,G.Jane,Jack', 'Jane') then 
    ...

ContainsText returns true if the subtext is found, without case-sensitivity, in the given text

In StrUtils you'll also find handy functions like StartsText, EndsText and ReplaceText

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Jens Borrisholt
  • 6,174
  • 1
  • 33
  • 67
  • Thanks, I am using like if Pos(content , container ) > 0 then showmessage('exists') else showmessage('Not exists'); But is there any other way – delsql Oct 29 '15 at 09:27
  • 1
    I don't understand your question, because so it would in Java – Jens Borrisholt Oct 29 '15 at 09:42
  • 3
    @delsql You are making something of a mess of this. Please don't ask new questions in comments to answers. If you want something different from `containsIgnoreCase` then, that would be a new question. – David Heffernan Oct 29 '15 at 10:08
  • 7
    FWIR ContaintsText in Delphi 7 (mentioned by the topic starter ) only works for LATIN-1 characters set. If @delsql wants to compare some national characters (Greek, Russian, etc) he should use `AnsiContainsText` instead – Arioch 'The Oct 29 '15 at 12:06
  • 1
    For the ContainsText function, the needle is the 2nd parameter, not the first. For this example: if ContainsText('A.Sven,G.Jane,Jack', 'Jane') then – Optavius Jul 18 '16 at 11:36
5

You might also find helpful the Contains Function in System.SysUtils as below.

uses
  Sysytem.SysUtils;

....
  txt := 'This is a string variable';

  if txt.contains('str') then
....
codeGood
  • 175
  • 1
  • 8