String Manipulation Basics: Working with Text in C#

Thungex
String Manipulation Basics: Working with Text in C#
String Manipulation in C#

Think of strings as any bit of text you want to use in your code. A string could be a word, a sentence, or even a number as long as it’s wrapped in quotes. Whenever you’re handling names, messages, or any text data, strings are your go-to in C#.

Let’s walk through some simple ways to work with text in C# without making it feel too technical. You’ll find that with a few handy tricks, you can adjust, combine, or check strings with just a line or two of code.

Starting Simple: Putting Text Together

One of the most common things you’ll do with strings is combining them. For instance, if you have a first name and a last name, and you want to put them together, you can “add” the strings using +.

Here’s how it looks:

string firstName = "Sam";
string lastName = "Rivers";
string fullName = firstName + " " + lastName; // Joins first and last name

In this example, + combines firstName and lastName with a space between them. Now fullName will store “Sam Rivers.” Easy, right? Think of + as your quick way to connect any pieces of text you need to display together.

Changing Case: Shouting or Whispering Text

Sometimes, you might want to make text stand out by changing its case—like putting everything in ALL CAPS if you want it to feel loud. That’s where ToUpper() comes in handy. It turns everything in the string to uppercase.

string username = "coder123";
string loudUsername = username.ToUpper(); // Changes to CODER123

Now loudUsername holds “CODER123.” It’s like shouting in code! You also have the opposite option with ToLower(), which turns everything into lowercase if you want a softer look.

string quietName = username.ToLower(); // Changes to coder123

Both of these tricks are simple, but they can help a lot, especially when you want consistency in formatting—like making sure user input is case-insensitive.

Checking the Length: How Many Characters?

Another thing you’ll often need is checking how many characters a string has. Maybe you’re counting characters in a tweet or limiting a username to a certain length. The Length property tells you exactly how many characters are in a string.

string password = "safePass2024";
int passwordLength = password.Length; // Counts the characters in the password

In this example, passwordLength will store the total number of characters in password—in this case, 11. It’s a simple trick but useful whenever you need to know how long a string is.

Getting Creative with Real Examples

  1. Creating a Personalized Message

    string firstName = "Alex";
    string welcomeMessage = "Hello, " + firstName + "! Welcome to the program.";

    Here, welcomeMessage combines everything to give a personalized greeting. It’s a nice way to use + for joining text and creating a friendly message.

  2. Making Text Stand Out with Uppercase

    string announcement = "don’t forget the meeting";
    string loudAnnouncement = announcement.ToUpper(); // Changes to DON’T FORGET THE MEETING

    Now loudAnnouncement sounds like it’s yelling, which can be useful if you want something important to stand out.

  3. Counting Characters in a Username

    string username = "gamer99";
    int usernameLength = username.Length; // Finds the number of characters

    Here, you’ll know exactly how long the username is, which is handy if you’re setting a limit.

With these basics, you’re ready to play around with strings in C#. Go ahead, experiment with different strings in your own code, and see what you can create!

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.