DEV Community

Cover image for SOME METHODS IN .NET C#
Dilmurod Yaqubbayev
Dilmurod Yaqubbayev

Posted on

2 2 2 2 2

SOME METHODS IN .NET C#

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!!
Enter fullscreen mode Exit fullscreen mode

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}'");
    }
}

Enter fullscreen mode Exit fullscreen mode

RESULT:

Original: '   Hello, World!   '
Trimmed: 'Hello, World!'
Enter fullscreen mode Exit fullscreen mode

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}");
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Index of 'World': 7
Enter fullscreen mode Exit fullscreen mode

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#"
    }
}
Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
    }
}

Enter fullscreen mode Exit fullscreen mode

In this example, it checks if the string "document.pdf" ends with .pdf, and the result is true.




GENERAL CONCLUSION:

  1. Trim(); Trim() – Removes leading and trailing spaces from a string.
  2. IndexOf(); IndexOf(value) – Finds the first matching character or word in a string.
  3. Replace(); Replace("old", "new") – Replaces the old value with the new value in the string.
  4. Compare(); Compare(string, string) – Compares two strings.
  5. EndsWith(); EndsWith(string) – Checks if the string ends with the specified substring.

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (2)

Collapse
 
dilmurod_yaqubbayev_073a5 profile image
Dilmurod Yaqubbayev

Yes, of course, try, even if it's 1%, there will definitely be an increase.

Collapse
 
dilmurod_yaqubbayev_67e16 profile image
Dilmurod Yaqubbayev

there is a hang over the previous ones

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs