C# | Strings | .Contains() | Codecademy (2024)

Table of Contents
Syntax Example Codebyte Example FAQs

The .Contains() method determines whether a string includes a particular character or substring. It returns true if the character is included, otherwise the method returns false. There are additional parameters that can modify the comparison rules.

Syntax

// Determines whether the String includes a given characterString.Contains(char);// Determines whether the String includes a given stringString.Contains(string);// Determines whether the String includes a given character considering the type of comparisonString.Contains(char, comparisonType)// Determines whether the String includes a given string considering the type of comparisonString.Contains(string, comparisonType)

.Contains() takes the following arguments:

  • char is a single character.
  • string is a sequence of characters.
  • comparisonType is an enumeration value that allows to add specific rules to compare strings such as culture, case, and sort. Passing as an additional argument:
  • CurrentCulture determines whether strings match culture-sensitive criteria.
  • CurrentCultureIgnoreCase same as above and ignores the case.
  • InvariantCulture determines whether strings match culture-sensitive criteria and the invariant culture.
  • InvariantCultureIgnoreCase same as above and ignores the case.
  • Ordinal determines whether strings match using binary sort rules.
  • OrdinalIgnoreCase same as above and ignores the case.

Example

The following example shows how we can use .Contains() method

using System;

public class Example

{

public static void Main()

{

string stringToSeek = "The distance is nothing when one has a motive.";

string substring = "motive";

char character = 'l';

bool result;

// String.Contains(string)

result = stringToSeek.Contains(substring);

Console.WriteLine(result);

// String.Contains(char)

result = stringToSeek.Contains(character);

Console.WriteLine(result);

}

}

Here is the following output:

True

False

Codebyte Example

The example below determines whether the word helpful is included in the particular string.

us

Visit us

code

Hide code

Code

Output

Hide output

Hide output

Loading...

C# | Strings | .Contains() | Codecademy (2024)

FAQs

How to use the contains() method in C#? ›

Basic Syntax for Contains() Method

//YourString is the string you want to check //CheckString is the string you want to find bool result = YourString. Contains(CheckString); The function returns true if CheckString is found within YourString , and false if it is not. As simple as that!

How to check if a string has certain characters in C#? ›

In C#, String. Contains() is an instance method of the String class. It is used to find whether a given string contains characters. If it finds a matching substring in this particular string, it returns True .

How to check for a substring in C#? ›

The . Contains() method determines whether a string includes a particular character or substring. It returns true if the character is included, otherwise the method returns false . There are additional parameters that can modify the comparison rules.

How to get a portion of a string in C#? ›

You call the Substring(Int32) method to extract a substring from a string that begins at a specified character position and ends at the end of the string.

What does using () do in C#? ›

In C#, using statements define a new scope block. Any variables declared inside of a using statement are local to that block. At the end of that new scope the dispose method for that object is called. Another way to ensure the correct use of disposable classes is to use a try-finally block.

How to use where () in C#? ›

The “where” Keyword in C#

This keyword is typically used in conjunction with the “IEnumerable” interface to filter data based on a specific condition. var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var evenNumbers = numbers. Where(n => n % 2 == 0);

What is trim() in C#? ›

C# Trim() is a string method. This method is used to removes all leading and trailing white-space characters from the current String object. This method can be overloaded by passing arguments to it. Syntax: public string Trim() or public string Trim (params char[] trimChars)

How to check if a string ends with a substring in C#? ›

To determine whether a string ends with a particular substring by using the string comparison rules of the current culture, signal your intention explicitly by calling the EndsWith(String, StringComparison) method overload with a value of CurrentCulture for its comparisonType parameter.

How do I check if a string contains a substring? ›

You can use JavaScript's includes() method to check whether a string contains a substring. This will return true if the substring is found, or false if not.

What is the alternative to substring in C#? ›

Spans are a newer alternative to substrings in C#. They provide a more efficient way to manipulate strings. A span is essentially a “view” of a portion of a larger array or string, without actually creating a new instance of that portion.

How to find all substrings of a given string in C#? ›

Given a string as an input we need to find all the substrings present in the given string. We can find all the substrings from the given string using the Substring() method. This method returns a substring from the current string.

Why use substring in C#? ›

A substring is a part, or a portion of the original string. Think of it like taking a slice out of pie, but that pie is text, and the pie slice is the substring. In C#, the Substring method allows you to get this slice without crumbing the entire pie.

How does the Contains method work? ›

contains() method is declared in the List interface and implemented in the ArrayList class. It is used to check if the element is present in the specified ArrayList or not. The function returns a boolean value of true if the element is present and false if not.

What is the contains method of a List in C#? ›

public bool Contains (T item); Here, item is the object which is to be locate in the List<T>. The value can be null for reference types. Return Value: This method returns True if the item is found in the List<T> otherwise returns False.

Why we are using Contains in C#? ›

Contains(Char, StringComparison)

Returns a value indicating whether a specified character occurs within this string, using the specified comparison rules.

How to check if a file contains string in C#? ›

Contains() Method. In C#, String. Contains() is a string method. This method is used to check whether the substring occurs within a given string or not.

Top Articles
Should You Consolidate Debt Into a Mortgage Refinance?
Premium: Definition, Meanings in Finance, and Types
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Tyson Zemlak

Last Updated:

Views: 6549

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.