C# Example: Creating a string, manipulating it using string methods, and outputting the result - Biz Tech

C# Example: Creating a string, manipulating it using string methods, and outputting the result

Listen
using System;

class Program
{
    static void Main(string[] args)
    {
        string message = "hello world";
        string uppercase = message.ToUpper();
        string reversed = ReverseString(message);
        int index = message.IndexOf('o');
        string substring = message.Substring(6, 5);

        Console.WriteLine("Original message: " + message);
        Console.WriteLine("Uppercase: " + uppercase);
        Console.WriteLine("Reversed: " + reversed);
        Console.WriteLine("Index of 'o': " + index);
        Console.WriteLine("Substring: " + substring);
    }

    static string ReverseString(string input)
    {
        char[] charArray = input.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }
}
 

This example creates a string, manipulates it using string methods, and outputs the results to the console. The following steps are performed:

  1. Define a string with the value “hello world”.
  2. Convert the string to uppercase using the ToUpper() method and store the result in a new string variable.
  3. Reverse the string using a custom ReverseString() method and store the result in a new string variable.
  4. Find the index of the letter ‘o’ in the string using the IndexOf() method and store the result in an integer variable.
  5. Extract a substring of the string using the Substring() method and store the result in a new string variable.
  6. Output the original string and the manipulated strings to the console using Console.WriteLine().

Note that there are many other string methods and operations you can perform, such as replacing text, formatting, and splitting. This example is just one possible multi-step process for working with strings.