0

I'm working on a function, which works fine except when I try to use the Substring method.

Once I add .substring(#,#) to any of the strings in my function, other strings seem to break.

Am I doing something wrong? I can't figure out a way to use substring() in a function without it breaking the function.

If I run my code outside of a function, everything seems to work fine.

function Get-String ($string1, $string2)
{
    return($string1.substring(0,1) + $string2)
}

Get-String("One"+"Two")

I'd expect this function to return "OTwo" It returns "O"

Replacing the variables with string literals resolves the issue.

2
  • 1
    You are concatenating the two strings into one string in the function call. Use Get-String "One" "Two" instead. Don't use brackets there, simply state the parameters separated by space. Commented Jul 17, 2019 at 13:53
  • Doh! It seems so obvious now. - thanks Theo! Commented Jul 17, 2019 at 13:59

1 Answer 1

2

Per Theo's comment: changed

Get-String("One"+"Two")

to

Get-String "One" "Two"

and everything works perfectly now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.