Table of Contents
Examples of String Functions in C#
1. Clone()
2. CompareTo()
3. Contains()
4. EndsWith()
5. Equals()
6. GetHashCode()
7. GetType()
8. IndexOf()
9. ToLower()
10. ToUpper()
11. Insert()
12. Length
13. Replace()
14. Split()
15. Substring()
Conclusion

C# String Functions

Sep 03, 2024 pm 03:14 PM
c# c# tutorial

Strings are the most essential part of C# programming language, and also is one of the important data types in modern languages including C#. The string data type is defined in the .NET base class library and it is a collection of characters in which each character is a Unicode character. The keyword string is an object of System. String type, which is used to denote a sequential collection of characters that is called a text and the string.  The keywords consist of two types called string and String to declare string variables. Both string and String are comparably equal, so you can make use of whichever the naming convention you like better to define string variables. To avoid NullReferenceException, by initializing strings with the Empty value in case of null.

Examples of String Functions in C#

Predefined string functions are available in C# programming, Let’s see how to use string function in C# programming with the help of examples

1. Clone()

Clone returns an instance of String. In other words, it returns another copy of that data. The return value will be merely another view of similar data. The Clone() method does not take any parameters.

Example:

String _string1="StringFunctions";
String _string2 = (String)_string1.Clone();
// To display both strings
Console.WriteLine("String : {0}", _string1);
Console.WriteLine("Clone String : {0}", _string2);
Copy after login

Output:

String              : StringFunctions

Clone String    : StringFunctions

2. CompareTo()

CompareTo() method is used to compare the string instance with a particular String object. It checks whether the String occurrence appears in the same position as the particular string or not. Once comparing to strings it returns an integer value as output.

Example:

string _string1 = "Welcome";
string _string2 = " Welcome ";
string _string3 = "C# Coding";
Console.WriteLine(_string1.CompareTo(_string2));
Console.WriteLine(_string2.CompareTo(_string3));
Copy after login

Output:

0

1

3. Contains()

Contains() method is used to return a value signifying whether the particular substring presents within this string or not. If the particular substring is found in this string, it returns true otherwise false. The return value of this method is either true or false a Boolean value.

Example:

string _string1 = " Welcome ";
string _string2 = " Welcome ";
string _string3 = "StringFunctions";
Console.WriteLine(_string1. Contains(_string2));
Console.WriteLine(_string2. Contains(_string3));
Copy after login

Output:

True

False

4. EndsWith()

EndsWith() method is used to verify whether the particular string matches the end of this string or not. If the particular string is present at the end of this string, then the result will be true otherwise false. The return value of this method is either true or false a Boolean value.

Example:

string _string1 = " Welcome ";
string _string2 = " ome ";
string _string3 = "ing";
Console.WriteLine(_string1. EndsWith(_string2));
Console.WriteLine(_string2. EndsWith(_string3));
Copy after login

Output:

True

False

5. Equals()

Equals() method is used to compare whether two particular String objects have an identical value or not. If both strings have similar value, it returns true otherwise false. The return value of the Equals() method is either true or false a Boolean value.

Example:

string _string1 = " Welcome ";
string _string2 = " Welcome ";
string _string3 = "Strings";
Console.WriteLine(_string1. Equals(_string2));
Console.WriteLine(_string2. Equals(_string3));
Copy after login

Output:

True

False

6. GetHashCode()

GetHashCode() method is used to getting the hash code of a specified string. It returns an integer value. The return value of GetHashCode() is the hash code of a string object.

Example:

string _ string1 = "String Functions";
Console.WriteLine(_string1.GetHashCode());
Copy after login

Output:

1085385658

7. GetType()

GetType() method is used to obtain the type of current object. It returns the System. Type of current instance which is used for reflection.

Example:

string _string1 = "String Functions";
Console.WriteLine(_string1.GetType ());
Copy after login

Output:

System.String

8. IndexOf()

IndexOf() is used to get the index of the particular character present in the string. It returns the index position of the first occurrence of a particular character as an integer value.

Example:

string _string1 = "String Functions";
int index = _string1.IndexOf('t');
Console.WriteLine(index);
Copy after login

Output:

1

9. ToLower()

This C# string function is used to convert a string into lowercase. It returns a string in lower case. The return value of ToLower () is a string.

Example:

string _string1 = "String Functions";
string _string2 = _string1.ToLower();
Console.WriteLine(_string2 );
Copy after login

Output:

string functions

10. ToUpper()

ToUpper() method is used to convert the string into uppercase. The return value of ToUpper () is a string.

Example:

string _string1 = "String Functions";
string _string2 = _string1.ToUpper();
Console.WriteLine(_string2 );
Copy after login

Output:

STRING FUNCTIONS

11. Insert()

Insert() method is used to insert the particular string at a specified index number. The index number starts from 0. After inserting the particular string, it returns a new modified string. The return value of Insert() is a new modified string.

Example:

string _string1 = "String Functions";
string _string2 = _string1.Insert(6,"-");
Console.WriteLine(_string2 );
Copy after login

Output:

String- Functions

12. Length

Length is a string property that returns a number of characters in a string and here spaces count as characters.

Example:

string _string1 = "String Functions";
Console.WriteLine(_string1.Length);
Copy after login

Output:

16

13. Replace()

This string function in C# is used to replaces the character to get another string in which all occurrences of a particular character in this string are replaced with another specified character.

Example:

string _string1 = "Strings in F#";
string _string2 = _string1.Replace('F','C');
Console.WriteLine(_string2 );
Copy after login

Output:

Strings in C#

14. Split()

Split() method is used to split the string based on the specified value of characters in an array. The return value of this method is the string array.

Example:

string _string1 = "Welcome C Sharp";
string[] _string2 = _string1.Split(' ');
foreach (string _string3 in _string2)
{
Console.WriteLine(_string3);
}
Copy after login

Output:

Welcome
C
Sharp

15. Substring()

SubString() method is used to retrieve a substring from the current occurrence of the String. The parameter “startIndex” will denote the initial position of substring and then substring will continue to the end of the string. The return value type is System. String.

Example:

string _string1 = " Hello C Sharp";
string _string2 = _string1.Substring(5);
string _string3 = " StringFunction";
string _string4 = _string3.Substring(0,8);
string _string5 = " StringFunction";
string _string6 = _string5.Substring(6,4);
Console.WriteLine(_string2);
Console.WriteLine(_string4);
Console.WriteLine(_string6);
Copy after login

Output:

C Sharp

StringFu

Func

Conclusion

 In this article, we learned the basics of strings in C# and how to use the String functions available in C#. Hope this article would have helped out you in understanding String Methods using C#

The above is the detailed content of C# String Functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

See all articles