Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# String Methods


Share

String Methods

You have already used some string methods, like ToUpper(), IndexOf() and Substring().

C# has many more. In this chapter, you will learn a few that you will use often: check if a string contains some text, check how it starts or ends, replace text, and remove extra spaces.

The Contains() method checks if a string contains some text. It returns True or False:

Example

string txt = "I love apples";
Console.WriteLine(txt.Contains("apples"));  // True
Console.WriteLine(txt.Contains("pears"));   // False
Console.WriteLine(txt.Contains("Apples"));  // False

Try it Yourself »

Note that Contains() is case-sensitive: "Apples" with an uppercase A is not the same as "apples".


StartsWith() and EndsWith()

The StartsWith() method checks if a string starts with some text, and EndsWith() checks if it ends with some text.

For example, a file name that ends with .jpg is an image:

Example

string fileName = "photo.jpg";
Console.WriteLine(fileName.StartsWith("photo"));  // True
Console.WriteLine(fileName.EndsWith(".jpg"));     // True
Console.WriteLine(fileName.EndsWith(".png"));     // False

Try it Yourself »


Replace()

The Replace() method replaces some text with other text. It replaces every match in the string:

Example

string colors = "red, blue, red";
Console.WriteLine(colors.Replace("red", "green"));  // green, blue, green

Try it Yourself »



Trim()

The Trim() method removes spaces from the start and the end of a string. This is useful when someone types a name with extra spaces.

In the example below, we print brackets around the text, so you can see where the spaces are:

Example

string name = "   Liam   ";
Console.WriteLine("[" + name + "]");         // [   Liam   ]
Console.WriteLine("[" + name.Trim() + "]");  // [Liam]

Try it Yourself »


The Original String Does Not Change

String methods do not change the original string. They return a new string with the result:

Example

string txt = "I like cats";
string newTxt = txt.Replace("cats", "dogs");
Console.WriteLine(txt);     // I like cats
Console.WriteLine(newTxt);  // I like dogs

Try it Yourself »

To keep the result, store it in a variable. It can also be the same variable: txt = txt.Replace("cats", "dogs");


Common String Methods

Here are some common string methods:

Method Description
Contains(text) Returns True if the string contains the text
StartsWith(text) Returns True if the string starts with the text
EndsWith(text) Returns True if the string ends with the text
IndexOf(text) Returns the position of the first match, or -1 if the text is not found
Replace(oldText, newText) Returns a new string where every oldText is replaced with newText
Substring(start) Returns the characters from position start to the end
Substring(start, length) Returns length characters, starting at position start
ToUpper() Returns the string in uppercase letters
ToLower() Returns the string in lowercase letters
Trim() Returns the string without spaces at the start and the end
TrimStart() Returns the string without spaces at the start
TrimEnd() Returns the string without spaces at the end


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.