ASSALAMU ALAYKUM DEAR DEVELOPER!! TODAY WE WILL SEE WITH YOU SOME METHODS IN .NET C#
In a state that supported this small work of ours
subscribe and don't forget to click on the heart, it's definitely a motivation for us!!
PLAN:
1) Trim();
2) IndexOf();
3) Replace();
4) Compare();
5) EndsWith();
1) Trim(); -- METHOD
BRIEF DESCRIPTION:
In the C# programming language, the Trim() method belongs to the string data type and is used to remove whitespace from the beginning and end of a string. This method is especially useful when processing user input or text cleaning.
Trim()
principle of operation of the method;
Trim()
method is called on the string object and returns a new string. For example:
using System;
class Program
{
static void Main()
{
string text = " Hello, World! ";
string trimmedText = text.Trim();
Console.WriteLine($"Original: '{text}'");
Console.WriteLine($"Trimmed: '{trimmedText}'");
}
}
RESULT:
Original: ' Hello, World! '
Trimmed: 'Hello, World!'
As you can see, the Trim() method removed the leading and trailing spaces of the string.
2) IndexOf() -- METHOD
BRIEF DESCRIPTION:
In C# programming language IndexOf()
method is used to determine at which index a certain character or word is located in a string. This method returns the zero-based index of the first match found. -1
is returned if the searched value is not found.
Main use:
using System;
class Program
{
static void Main()
{
string text = "Hello, World!";
int index = text.IndexOf("World");
Console.WriteLine($"Index of 'World': {index}");
}
}
Result:
Index of 'World': 7
Since indexing starts from 0
, "World" begins at position 7
in the string.
3) Replace() -- METHOD
;
BRIEF DESCRIPTION:
Replace() Method
The Replace() method in C# is used to replace a specified character or substring with another character or substring within a string. It returns a new string with the replacements, but the original string remains unchanged, as strings are immutable in C#.
Basic Usage:
using System;
class Program
{
static void Main()
{
string text = "Hello, world!";
string newText = text.Replace("world", "C#");
Console.WriteLine(newText); // Output: "Hello, C#"
}
}
This code replaces the word "world" with "C#" and the result is "Hello, C#".
Replacing a Single Character:
You can also use Replace() to replace individual characters:
string text = "banana";
string newText = text.Replace('a', 'o');
Console.WriteLine(newText); // Output: "bonono";
Here, all 'a' characters are replaced with 'o'.
4) Compare() -- METHOD;
BRIEF DESCRIPTION:
Compare() Method
The Compare() method in C# is used to compare two strings and determine their relative order. This method returns either 0, a negative value, or a positive value depending on how the strings compare:
0 — The strings are equal.
Negative value — The first string is less than the second string.
Positive value — The first string is greater than the second string.
Basic Usage:
using System;
class Program
{
static void Main()
{
string text1 = "apple";
string text2 = "banana";
int result = String.Compare(text1, text2);
Console.WriteLine(result); // Output: Negative value
}
}
In this case, the result is a negative value because "apple" is less than "banana".
Equality Check
If the two strings are equal, the Compare() method will return 0:
string text1 = "apple";
string text2 = "apple";
int result = String.Compare(text1, text2);
Console.WriteLine(result); // Output: 0
Here, since both strings are identical, the result is 0.
5) EndsWith() -- METHOD;
BRIEF DESCRIPTION:
EndsWith() Method
The EndsWith() method in C# is used to check if a string ends with a specified substring or character(s). This method returns a bool value:
true — If the string ends with the specified substring or character(s).
false — If the string does not end with the specified substring or character(s).
Basic Usage:
using System;
class Program
{
static void Main()
{
string fileName = "document.pdf";
bool result = fileName.EndsWith(".pdf");
Console.WriteLine(result); // Output: true
}
}
In this example, it checks if the string "document.pdf" ends with .pdf, and the result is true.
GENERAL CONCLUSION:
- Trim();
Trim() – Removes leading and trailing spaces from a string.
- IndexOf();
IndexOf(value) – Finds the first matching character or word in a string.
- Replace();
Replace("old", "new") – Replaces the old value with the new value in the string.
- Compare();
Compare(string, string) – Compares two strings.
- EndsWith();
EndsWith(string) – Checks if the string ends with the specified substring.
Top comments (0)