Heim > Backend-Entwicklung > C#.Net-Tutorial > C#-String-Funktionen

C#-String-Funktionen

王林
Freigeben: 2024-09-03 15:14:01
Original
828 Leute haben es durchsucht

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);
Nach dem Login kopieren

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));
Nach dem Login kopieren

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));
Nach dem Login kopieren

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));
Nach dem Login kopieren

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));
Nach dem Login kopieren

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());
Nach dem Login kopieren

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 ());
Nach dem Login kopieren

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);
Nach dem Login kopieren

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 );
Nach dem Login kopieren

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 );
Nach dem Login kopieren

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 );
Nach dem Login kopieren

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);
Nach dem Login kopieren

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 );
Nach dem Login kopieren

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);
}
Nach dem Login kopieren

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);
Nach dem Login kopieren

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#

Das obige ist der detaillierte Inhalt vonC#-String-Funktionen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage