CategoryString

String Part 2

Substring

A substring is any sequence of characters that is contained in a string.  We use Substring method when we want to create a new string that is based on original string. Replace method is used when we want to change parts of original string and create new one. IndexOf is used when we want to locate first occurrence of certain substring in string.

 

How to create new string using Multiple existing strings?

That process is called concatenation and all it basically does is append one string at the end of another string. In next example we will create 3 string and concatenate them. For this example Gangsta Lorem Ipsum is used.

As you can see new string is created using simple addition. Notice on third operation ” “, we have created a new string with only one blank space. Why? Because when we create new string we have to add it ourselves. Look at the end of first string, you can see that there is empty space at the end, so we did not have to add between firstString and secondString .

 

That’s all for now regarding string in future we will talk some more about them.

Tomorrow we will talk about reference types so be sure co check in again!

 

String Part 1

Strings are one of the most useful types in .Net, and since you’ll be using it almost every day, let’s see what we can learn about String.

MSDN: A string is a sequential collection of Unicode characters that is used to represent text.

Strings are a reference type but in some ways it behaves like a value type. For instance, String is immutable, meaning you cannot change content of a string.

Strings can be null.

You can compare two strings.

Maximum size of a string is 2Gb.

Common Operations with Strings

Strings are hard-coded with literals. There are two types of literals: “regular literal” and “verbatim literal”.

If you try running a program, the output will be the same. So how do we know which type of literal to use? When you have a simple text that can be written in one line and does not contain escape characters, you will typically use regular expressions.  Verbatim literals are for example useful when you write SQL code.

Let’s try out some common operations with strings using the following well-known sentence:  The quick brown fox jumps over the lazy dog.

\n –  newline

\” – double quotes

\’ – single quote

\\ – backslash

 

Trim text

A very common operation, and all it does is basically remove empty space at the beginning, end or both. Remember, because strings are immutable, String itself will not be changed.

 

How to Check if String Is Empty

All the scenarios above bring about the same result. But there are a few things to be aware of. Length method is the fastest one, both Length and Empty methods create the same code in IL, but in my opinion, the last approach is the easiest one to read and it also checks the strings for null.

 

Thank you for following me so far and be sure to check in tomorrow because in the next post we will continue covering Strings.

 

© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑