Back to Blog
Author: Published 39 views
C# Console Examples 2

C# Console Examples 2

Learn C# Console string methods with practical examples for trimming, searching, splitting, replacing and formatting, including code and outputs.

In C# Console Examples 2, we focus on the core string methods that make text processing easier. This example does not only list method names; it also shows the exact console output.

Example: Cleaning, searching and splitting strings

The console app below demonstrates Trim, ToUpper, ToLower, Length, Contains, StartsWith, EndsWith, IndexOf, Substring, Replace, Split and Join in one simple flow.

CSHARP
using System;

class Program
{
    static void Main()
    {
        string title = "  C# Console String Methods  ";
        string email = "support@maximumyazilim.com";
        string tags = "csharp, console, string, method";

        string cleanTitle = title.Trim();

        Console.WriteLine("Original: [" + title + "]");
        Console.WriteLine("Trim: [" + cleanTitle + "]");
        Console.WriteLine("ToUpper: " + cleanTitle.ToUpper());
        Console.WriteLine("ToLower: " + cleanTitle.ToLower());
        Console.WriteLine("Length: " + cleanTitle.Length);

        Console.WriteLine("Contains Console: " + cleanTitle.Contains("Console"));
        Console.WriteLine("StartsWith C#: " + cleanTitle.StartsWith("C#"));
        Console.WriteLine("EndsWith Methods: " + cleanTitle.EndsWith("Methods"));

        int atIndex = email.IndexOf("@");
        string domain = email.Substring(atIndex + 1);
        Console.WriteLine("Domain: " + domain);

        string fixedTags = tags.Replace("method", "example");
        string[] parts = fixedTags.Split(", ");
        Console.WriteLine("Join: " + string.Join(" | ", parts));

        string name = "Alice";
        Console.WriteLine($"{name} learned {cleanTitle}.");
    }
}

Output

OUTPUT
Original: [  C# Console String Methods  ]
Trim: [C# Console String Methods]
ToUpper: C# CONSOLE STRING METHODS
ToLower: c# console string methods
Length: 25
Contains Console: True
StartsWith C#: True
EndsWith Methods: True
Domain: maximumyazilim.com
Join: csharp | console | string | example
Alice learned C# Console String Methods.

What do these methods do?

  • Trim removes spaces from the beginning and end of a string.
  • ToUpper and ToLower convert text to uppercase or lowercase.
  • Length returns the number of characters in the string.
  • Contains, StartsWith and EndsWith check whether the text matches a specific expression.
  • IndexOf and Substring help you extract a specific part of the text.
  • Replace changes one expression inside the text with another expression.
  • Split separates text into parts, while Join combines those parts again.

Short answers

  • Are C# string values mutable? No, strings are immutable; most string methods return a new string value.
  • Where are string methods used in console apps? They are useful for cleaning user input, checking values, splitting text and printing cleaner output.
  • Can I copy and run the code? Yes, use the Copy code button and paste it into Program.cs in a new C# Console project.