- C# Tutorial
- C# Tutorial
- C# Basic Syntax
- C# Operators
- C# if else
- C# switch
- C# Loops
- C# break Vs. continue
- C# Arrays
- C# Strings
- C# Methods
- C# Examples
- C# Add Two Numbers
- C# Swap Two Numbers
- C# Reverse a Number
- C# Reverse a String
- C# Celsius to Fahrenheit
- Computer Programming
- Learn Python
- Python Keywords
- Python Built-in Functions
- Python Examples
- Learn C++
- C++ Examples
- Learn C
- C Examples
- Learn Java
- Java Examples
- Learn Objective-C
- Web Development
- Learn HTML
- Learn CSS
- Learn JavaScript
- JavaScript Examples
- Learn SQL
- Learn PHP
Strings in C# with Examples
To store textual data in a variable in C#, we have to use the string type. Strings in C# are combinations of characters. For example:
- "C# is fun!"
- "fresherearth"
- "Hey buddy, what's up?"
Please note: Strings are enclosed within a quote.
Here is an example that shows how we can create a string type variable in C#.
string myvar;
To initialize any string value to a string type variable, just quote the string or text and initialize it to the variable in this way.
string myvar = "jobails.com";
C# String Example
Now let me create an example in C# that demonstrates strings. This example will receive input from the user and print the entered data back on the output console.
Console.WriteLine("Enter anything: "); string mytext = Console.ReadLine(); Console.WriteLine("You entered: " + mytext);
The snapshot given below shows the sample run of the above C# example after executing it. I took this snapshot after executing this C# code in Microsoft Visual Studio.
I used a red line to draw your attention to the output text that is related to the program. I have used "Learning C# is Fun!" as input.
Find Length of String in C#
To find the length of a specified string, use the C# Length property. Here is an example demonstrating the use of the Length property.
Console.WriteLine("Enter the String: "); string str = Console.ReadLine(); Console.WriteLine("\nLength of the String = " + str.Length);
The sample run with user input "fresherearth" should exactly be
Convert Uppercase String to Lowercase in C#
To convert an uppercase string to its lowercase counterpart, use the ToLower() method in this way.
string x = "fresherearth"; string y = "C# Programming is Fun!"; Console.WriteLine(x.ToLower()); Console.WriteLine(y.ToLower());
The output should exactly be:
fresherearth c# programming is fun!
Convert Lowercase String to Uppercase in C#
To convert an lowercase string to its uppercase counterpart, use the ToUpper() method in this way.
string x = "fresherearth"; string y = "C# Programming is Fun!"; Console.WriteLine(x.ToUpper()); Console.WriteLine(y.ToUpper());
The output should exactly be:
fresherearth C# PROGRAMMING IS FUN!
String Concatenation in C#
String concatenation in C# can be accomplished in this manner by using the Concat() method.
string x = "fresherearth"; string y = "C# Programming is Fun!"; Console.WriteLine(string.Concat(x, y));
The output should exactly be:
fresherearthC# Programming is Fun!
You can also use the + operator to perform the string concatenation in this way.
string x = "fresherearth"; string y = "C# Programming is Fun!"; Console.WriteLine(x + y);
You will get the same output as the previous example's output.
« Previous Tutorial Next Tutorial »