Search code examples
powershellvariables

Did I do this PowerShell problem correctly?


Can someone check to see if I did this PowerShell problem correct? Also, if you answer it could please explain what I did wrong. That will help me to continue to learn. Thanks!

Problem 3.) Concatenate a and b so that it creates variable $c such that c -eq "this is the beginning this is the ending" Print the value of variable c.

Here is what I tried and the output was: False

$c = "this is the beginning this is the end"
($a + $b) -eq $c

Solution

  • As per my comment, and my assumptions made.

    Clear-Host
    $a = 'this is the beginning'
    $b = 'this is the end'
    $c = 'this is the beginning this is the end'
    
    ($a + $b) -eq $c
    # Results
    <#
     ($a + $b) -eq $c
    False
    #>
    
    # Note the space added for the equality of the full string
    ($a + ' ' + $b) -eq $c
    # Results
    <#
    True
    #>
    
    # Or
    ("$a " + $b) -eq $c
    # Results
    <#
    True
    #>
    
    
    # Single quote is a literal string
    # Double quote is normally used for string expansion or PowerShell variable, or formatting requirements.