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
10 Tips For Living Happily on One Income
11 Simple Financial Tips You Should Have Followed 30 Years Ago | The Motley Fool
Po Box 7250 Sioux Falls Sd
Kansas City Kansas Public Schools Educational Audiology Externship in Kansas City, KS for KCK public Schools
Citibank Branch Locations In Orlando Florida
The Ivy Los Angeles Dress Code
THE 10 BEST River Retreats for 2024/2025
Nyuonsite
Red Heeler Dog Breed Info, Pictures, Facts, Puppy Price & FAQs
Richmond Va Craigslist Com
Wordle auf Deutsch - Wordle mit Deutschen Wörtern Spielen
The Murdoch succession drama kicks off this week. Here's everything you need to know
Leader Times Obituaries Liberal Ks
Lehmann's Power Equipment
Td Small Business Banking Login
Busted Campbell County
Poe Str Stacking
Melendez Imports Menu
Little Rock Skipthegames
Best Boston Pizza Places
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
Pacman Video Guatemala
Jamielizzz Leaked
CohhCarnage - Twitch Streamer Profile & Bio - TopTwitchStreamers
Eegees Gift Card Balance
Fairwinds Shred Fest 2023
Melissa N. Comics
How to Use Craigslist (with Pictures) - wikiHow
Unm Hsc Zoom
Pnc Bank Routing Number Cincinnati
The Ride | Rotten Tomatoes
Tgh Imaging Powered By Tower Wesley Chapel Photos
Car Crash On 5 Freeway Today
John F Slater Funeral Home Brentwood
Chatropolis Call Me
Craigslist Ludington Michigan
Skip The Games Grand Rapids Mi
Tyler Perry Marriage Counselor Play 123Movies
Clausen's Car Wash
Linkbuilding uitbesteden
Powerspec G512
Catchvideo Chrome Extension
Craigslist Mendocino
Terrell Buckley Net Worth
Verizon Forum Gac Family
Maurices Thanks Crossword Clue
Minecraft Enchantment Calculator - calculattor.com
Basic requirements | UC Admissions
Obituary Roger Schaefer Update 2020
Ihop Deliver
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.