locked
connencting a count-variable within two strings RRS feed

  • Question

  • I have the following problem:

    I have two strings: $stringA, $stringB and a variable $i with an int value.

    I use them in a for loop.

    $StringA and $StringB have always the same values.

    $i changes it's values due to the loop.

    But the result within the loop is the following:

    $StringA$i$i$i$i$i$i$i$i$i$i$i$i$i$i$i$i$i....$StringB

    and not as expected:

    $StringA$i$StringB 

    $StringA$i$StringB 

    $StringA$i$StringB 

    $StringA$i$StringB 

    $StringA$i$StringB 

    $StringA$i$StringB 

    Has anyone an idea?

    Thursday, August 10, 2017 1:55 PM

All replies

  • Well since you did not post the actual script or for loop, kind of hard to guess

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, August 10, 2017 2:06 PM
  • It's pretty simple but we have to see code. You probably adding it to the string instead of combining it all together. If you assign a number to a variable it treats it as double or int. Adding it to a string won't work.

    This is an example what works. 

    $i=1
    $StringA = 'StringA'
    $StringB = 'StringB'
    
    while ($i -le 100) {
    $I = $I + 1
    
    "$StringA$I$StringB"
    
    # or
    $StringA + $I + $StringB
    
    # or
    "$($StringA)$I$($StringB)"
    
    }


    Cheers,

    Ruud
    Twitter:    Blog: AzureStack.Blog  LinkedIn:    
    Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    Thursday, August 10, 2017 3:32 PM
  • I'm new to powershell, that's what i did:

    $stringA='start'
    $stringB='end'
    
    for($i=0;$i -le 100; $i++){
    
    $stringA+$i+$stringB
    #also tried this:
    $string='beginning of it'+$i+'end of it'
    
    }

    I have to build a query with an id in it, that i have to change.


    • Edited by newbiehero Thursday, August 10, 2017 4:18 PM
    Thursday, August 10, 2017 4:15 PM
  • I just ran your script above and my output is as follows, so what is not working properly as that is the output you said you wanted in the original post

    start0end
    start1end
    start2end
    start3end
    start4end
    start5end
    etc...



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Thursday, August 10, 2017 4:45 PM typo
    Thursday, August 10, 2017 4:35 PM
  • Your code produces the output you wanted.

    $StringA$i$StringB 

    $StringA$i$StringB 

    $StringA$i$StringB 

    $StringA$i$StringB 

    $StringA$i$StringB 

    $StringA$i$StringB 


    Cheers,

    Ruud
    Twitter:    Blog: AzureStack.Blog  LinkedIn:    
    Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Thursday, August 10, 2017 4:38 PM
  • KiSS will help

    1..100|%{"start$_`end"}


    \_(ツ)_/

    Thursday, August 10, 2017 5:12 PM
  • Hi,
    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,

    Frank

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, September 6, 2017 7:45 AM