DEV Community

Cover image for Elaborating on Strings
Kyle Martin
Kyle Martin

Posted on

Elaborating on Strings

Introduction
Hello anyone who reads this. I am aiming to make short read posts on dev.to to teach members about the C sharp programming language. I have recently started a blog on Wordpress to record my learnings and thoughts on computer programming. This has caused me to write summarized posts on dev in order to hopefully teach someone out there something and if they are interested in learning more they can read the more detailed post on my blog.

Table of Contents:

  1. Learn C#: Variables
  2. Learn C#: Converting variables
  3. This post

The String Type
The string type is a variable that contains zero or more unicode characters. Just like other variables it must be declared, can be initialized, and assigned. Strings are immutable data which means it cannot be changed once created. Any operation that attempts to modify the assignment will end up creating a new string object

string definition = "This is a string";

In the example above I declared a string variable using the string alias and named the variable definition. Then I assigned a series of characters.

string definition = null; // initializing as null

string definition = ""; // initializing with zero characters

Both of the examples above are other ways to initialize a string variable in C#.


Earlier I mentioned 'alias'. Essentially, what I meant is when you declare a string using the lowercase version instead of uppercase version, you are calling the System.String class using an alias.


String Methods
There are many properties or methods you can use with string variables to manipulate the data within your application.

  • Retrieve characters in a string using indexing
  • String Interpolation
  • String Variable Lengths
  • String Comparison
  • String Concatenation
  • String Formatting & Modification (not mentioned in blog post)

String Length

string greeting = "Hello!";
int stringLength = greeting.Length; // Output: 6
Enter fullscreen mode Exit fullscreen mode

Above is the example on how to use the length property of the string class. It returns an integer that describes the length of the string. This uses a zero-based counting system (0, 1, 2, etc...).


String Concatenation
Sometimes, you would like to combine two strings together to formulate a new string.

string firstName = "Kyle";
string lastName = "Martin";
string fullName = firstName + " " + lastName;
// Or
string fullName = string.Concat(firstName, " ", lastName);
Enter fullscreen mode Exit fullscreen mode

This will output my full name when calling the fullName variable. I encourage you to create a new console application and give this a shot. See what small applications you can create with String Concatenation.


To see the other methods please head on over to my blog post as it is a much longer read and contains additional resources to help you learn more about the String Class in C#.


Thank you for your time. Again, I am trying to keep these dev.to posts small in order to garner what little precious time I can. If this intrigues you, please visit my blog.

https://kylemartin0819.wordpress.com/2023/05/11/reference-types-and-string-variables-in-c/

Also, please consider leaving a post emoji, comment, or follow. I also have a link to my GitHub where I will be improving overtime to contain my programming language learnings and applications that I build.

  • Kyle

Top comments (0)