DotNet-100-PreWork-GitBook
  • DotNet PreWork
  • Part 0: Intro
    • 1.0: Installation
    • 2.0: Getting Started
    • 3.0: Configuration
  • Part 1: HTML Fundamentals
    • Basic Tags
    • H & P Tags
    • Lists
    • Links
    • Images
    • Tables
    • Divs
    • Inputs
    • Forms
    • Sections
    • Articles & Headers
    • Footers
    • Navs
    • iFrames
  • Part 2: CSS Fundamentals
    • CSS Set Up
    • Classes
    • Ids
    • Margins & Padding
    • Fonts
    • Backgrounds
    • Widths & Heights
    • Borders
    • Positions
  • Part 3: C# Fundamentals
    • 1.0: Hello World
    • 2.0: Variables
    • 3.0: Basic Types
    • 3.0a: Types Challenges
    • 3.0b: Types Challenges Answers
    • 4.0: Operators
    • 5.0: Strings
    • 6.0: Booleans
      • 6.0a: Boolean Challenge
      • 6.0b: Challenge Answers
    • 7.0: Conditionals
      • 7.1: Switch Case
      • 7.2: If Statements
      • 7.3: Ternary Expressions
    • 8.0: Numbers
    • 9.0: Loops
      • 9.0a: Loop Challenges
      • 9.0b: Challenge Answers
    • 10.0: Classes and Objects
    • 11.0: Properties
    • 12.0: Useful-Links
Powered by GitBook
On this page
  • STRINGS
  • Description
  • File Location
  • Declaring and Initializing Strings
  • Answers
  • Joining Strings
  • Three Types of string manipulation
  • Concatenation
  • Composite Formatting:
  • String Interpolation:
  • Challenge
  1. Part 3: C# Fundamentals

5.0: Strings

STRINGS

In this module we'll study String types in more depth, and study three ways to manipulate strings.

Description

Strings, as briefly mentioned in a previous module, are just a collection of characters that can be stored and used throughout an application in different ways. Almost any text, single character, or number can be a string in C#. Strings are initialized by using double quotes in C#.

File Location

  1. Right click on the solution. Click Add > New Project.

  2. Select Console App (.NET Framework).

  3. Name it 05_strings.

  4. Write your code for this module within the Program.cs of that project.

Declaring and Initializing Strings

See if you can remember how to do the following things:

  1. declare a string called myName, but don't give it a value yet.

  2. Declare a string and initialize it by giving it a value.

Answers

  1. string myName;

  2. string wholeName = "Douglas Crockford"

Joining Strings

In this module we'll explore common ways to manipulate strings.

Three Types of string manipulation

There are three common ways of manipulating strings in C#:

  • Concatentation

  • Composite Formatting

  • Interpolation

Concatenation

Let's start with string concatenation. In the Program.cs file in 05_strings, add the following code:

string first = "The cars we sell are ";
string second = "BMW, Lexus, and Mercedes.";
Console.WriteLine(first + second);

//result: The cars we sell are BMW, Lexus, and Mercedes.

With concatenation we use the + operator to combine two strings. Notice the space after are and before the closing ". If there was no space, you would need to add one, like this:

Console.WriteLine(first + " " + second);

Composite Formatting:

Composite formatting is another common way of manipulating strings. Add the following code to your Program.cs file:

string firstName = "Jenn";
string lastName = "Williams";
                                //1         //2
Console.WriteLine("Her name is {0} {1}.", firstName, lastName);

//result: Her name is Jenn Williams.

Take a note of a few details here: 1. Composite formatting uses curly braces inside the string and a number inside the curly brace. Notice that the first variable is a 0 value in the curly brace, like an array.

  1. A comma follows the string, then each variable name is added.

String Interpolation:

String interpolation is another common method of manipulating strings. Code and study the following:

string firstName = "Robin";
string lastName = "Holler";
                //1                 //2
Console.WriteLine($"Her name is {firstName} {lastName}");

Note a few things: 1. String interpolation requires that we use the $ before the string. 2. The dollar sign allows us to pass variables in directly to the curly braces.

Challenge

  • Practice these 3 ways of joining strings together in your Program.cs file.

Previous4.0: OperatorsNext6.0: Booleans

Last updated 6 years ago