String.Contains Method (System) (2024)

Table of Contents
Examples Remarks See also Applies to
Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

Returns a value indicating whether a specified substring occurs within this string.

public: bool Contains(System::String ^ value);
public bool Contains (string value);
member this.Contains : string -> bool
Public Function Contains (value As String) As Boolean

Parameters

value
String

The string to seek.

Returns

true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.

Exceptions

value is null.

Examples

The following example determines whether the string "fox" is a substring of a familiar quotation. If "fox" is found in the string, it also displays its starting position.

using namespace System;int main(){ String^ s1 = "The quick brown fox jumps over the lazy dog"; String^ s2 = "fox"; bool b = s1->Contains( s2 ); Console::WriteLine( "Is the string, s2, in the string, s1?: {0}", b ); if (b) { int index = s1->IndexOf(s2); if (index >= 0) Console::WriteLine("'{0} begins at character position {1}", s2, index + 1); }}// This example displays the following output:// 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True// 'fox begins at character position 17
string s1 = "The quick brown fox jumps over the lazy dog";string s2 = "fox";bool b = s1.Contains(s2);Console.WriteLine("'{0}' is in the string '{1}': {2}", s2, s1, b);if (b) { int index = s1.IndexOf(s2); if (index >= 0) Console.WriteLine("'{0} begins at character position {1}", s2, index + 1);}// This example displays the following output:// 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True// 'fox begins at character position 17
let s1 = "The quick brown fox jumps over the lazy dog"let s2 = "fox"let b = s1.Contains s2printfn $"'{s2}' is in the string '{s1}': {b}"if b then let index = s1.IndexOf s2 if index >= 0 then printfn $"'{s2} begins at character position {index + 1}"// This example displays the following output:// 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True// 'fox begins at character position 17
Class Example Public Shared Sub Main() Dim s1 As String = "The quick brown fox jumps over the lazy dog" Dim s2 As String = "fox" Dim b As Boolean = s1.Contains(s2) Console.WriteLine("'{0}' is in the string '{1}': {2}", s2, s1, b) If b Then Dim index As Integer = s1.IndexOf(s2) If index >= 0 Then Console.WriteLine("'{0} begins at character position {1}", s2, index + 1) End If End If End SubEnd Class'' This example displays the following output:' 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True' 'fox begins at character position 17

Remarks

This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.

To perform a culture-sensitive or ordinal case-insensitive comparison:

  • On .NET Core 2.1 and later versions: Call the Contains(String, StringComparison) overload instead.

  • On .NET Framework: Create a custom method. The following example illustrates one such approach. It defines a String extension method that includes a StringComparison parameter and indicates whether a string contains a substring when using the specified form of string comparison.

using System;public static class StringExtensions{ public static bool Contains(this String str, String substring, StringComparison comp) { if (substring == null) throw new ArgumentNullException("substring", "substring cannot be null."); else if (! Enum.IsDefined(typeof(StringComparison), comp)) throw new ArgumentException("comp is not a member of StringComparison", "comp"); return str.IndexOf(substring, comp) >= 0; }}
open Systemopen System.Runtime.CompilerServices[<Extension>]type StringExtensions = [<Extension>] static member Contains(str: string, substring, comp: StringComparison) = if substring = null then invalidArg "substring" "substring cannot be null" if Enum.IsDefined(typeof<StringComparison>, comp) |> not then invalidArg "comp" "comp is not a member of StringComparison" str.IndexOf(substring, comp) >= 0
String s = "This is a string.";String sub1 = "this";Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);StringComparison comp = StringComparison.Ordinal;Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));comp = StringComparison.OrdinalIgnoreCase;Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));// The example displays the following output:// Does 'This is a string.' contain 'this'?// Ordinal: False// OrdinalIgnoreCase: True
let s = "This is a string."let sub1 = "this"printfn $"Does '{s}' contain '{sub1}'?"let comp = StringComparison.Ordinalprintfn $" {comp:G}: {s.Contains(sub1, comp)}"let comp2 = StringComparison.OrdinalIgnoreCaseprintfn $" {comp2:G}: {s.Contains(sub1, comp2)}"// The example displays the following output:// Does 'This is a string.' contain 'this'?// Ordinal: False// OrdinalIgnoreCase: True
Imports System.Runtime.CompilerServicesModule StringExtensions <Extension()> Public Function Contains(str As String, substring As String, comp As StringComparison) As Boolean If substring Is Nothing Then Throw New ArgumentNullException("substring", "substring cannot be null.") Else If Not [Enum].IsDefined(GetType(StringComparison), comp) Throw New ArgumentException("comp is not a member of StringComparison", "comp") End If Return str.IndexOf(substring, comp) >= 0 End FunctionEnd Module
Public Module Example Public Sub Main Dim s As String = "This is a string." Dim sub1 As String = "this" Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1) Dim comp As StringComparison = StringComparison.Ordinal Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp)) comp = StringComparison.OrdinalIgnoreCase Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp)) End SubEnd Module' The example displays the following output:' Does 'This is a string.' contain 'this'?' Ordinal: False' OrdinalIgnoreCase: True

If you are interested in the position of the substring value in the current instance, you can call the IndexOf method to get the starting position of its first occurrence, or you can call the LastIndexOf method to get the starting position of its last occurrence. The example includes a call to the IndexOf(String) method if a substring is found in a string instance.

See also

  • IndexOf(Char)
  • LastIndexOf(String)

Applies to

String.Contains Method (System) (2024)
Top Articles
Manage SSL/TLS protocols and cipher suites for AD FS
16-year-old college student and mom are both in school, take every single class together
Devin Mansen Obituary
Poe T4 Aisling
Matgyn
COLA Takes Effect With Sept. 30 Benefit Payment
Cumberland Maryland Craigslist
Www Thechristhospital Billpay
Strange World Showtimes Near Cmx Downtown At The Gardens 16
Lesson 2 Homework 4.1
Colts seventh rotation of thin secondary raises concerns on roster evaluation
Lima Funeral Home Bristol Ri Obituaries
Cashtapp Atm Near Me
Sky X App » downloaden & Vorteile entdecken | Sky X
60 X 60 Christmas Tablecloths
Does Breckie Hill Have An Only Fans – Repeat Replay
Second Chance Maryland Lottery
Soccer Zone Discount Code
Milspec Mojo Bio
Aspen Mobile Login Help
Watch The Lovely Bones Online Free 123Movies
Traveling Merchants Tack Diablo 4
Why do rebates take so long to process?
Craigslist St. Cloud Minnesota
Target Minute Clinic Hours
Myql Loan Login
Mals Crazy Crab
The 15 Best Sites to Watch Movies for Free (Legally!)
Temu Seat Covers
1400 Kg To Lb
Buhsd Studentvue
Wsbtv Fish And Game Report
Aliciabibs
Wisconsin Women's Volleyball Team Leaked Pictures
Skip The Games Grand Rapids Mi
Blackwolf Run Pro Shop
Xxn Abbreviation List 2023
Expendables 4 Showtimes Near Malco Tupelo Commons Cinema Grill
Big Reactors Best Coolant
Nimbleaf Evolution
Mauston O'reilly's
Lawrence E. Moon Funeral Home | Flint, Michigan
Bedbathandbeyond Flemington Nj
Runescape Death Guard
Unpleasant Realities Nyt
Hampton Inn Corbin Ky Bed Bugs
sin city jili
91 East Freeway Accident Today 2022
Access One Ummc
OSF OnCall Urgent Care treats minor illnesses and injuries
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6266

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.