How to concatenate array of integers into comma separated string

前端 未结 4 958
陌清茗
陌清茗 2021-01-22 01:55

I have two question, acually:

  1. How to join array of integers into comma separated string? (1,2,3) => \"1,2,3\"
  2. How to convert array of integers t
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 02:37

    Next code snippet could help out on understanding:

    $arraylist = New-Object 'system.collections.arraylist'
    $arraylist.Add(111)           | Out-Null
    $arraylist.Add([string]222)   | Out-Null
    $arraylist.Add('"' + 3 + '"') | Out-Null
    for($i=0; $i -lt $arraylist.Count; $i++ ){
        write-host $i, $arraylist[$i], $arraylist[$i].GetType()
    }
    write-host ''
    $csv = $arraylist -join ','
    $csv
    

    Output:

    0 111 System.Int32
    1 222 System.String
    2 "3" System.String
    
    111,222,"3"
    

    Additional view of (un)importance of " double quotes in a string type shows next + operation (sum of integers but concatenation of strings):

        write-host $i, $arraylist[$i], $arraylist[$i].GetType().Name, ($arraylist[$i] + 55) 
    

    gives next output:

    0 111 Int32 166
    1 222 String 22255
    2 "3" String "3"55
    
    111,222,"3"
    

    However, " double quotes have another important and meaningful role in .csv file when imported e.g. to Excel sheet.

提交回复
热议问题