Swift:将数组转换为字符串

时间:2017-05-10 16:33:08

标签: ios swift cocoa-touch foundation

如何将数组(例如[Int])转换为字符串,在元素之间保留逗号?如果我有一个像[1,2,3,4]这样的数组,我希望收到一个像"1, 2, 3, 4"这样的字符串。

2 个答案:

答案 0 :(得分:5)

你可以这样做:

$loss_res;
        if($result['total_loses'][$result['team_list'][$i]]!=''){
          $loss_res=$result['total_loses'][$result['team_list'][$i]];
        } else {
          $loss_res=0;
        }

$draw_res; if($result['team_drawn'][$result['team_list'][$i]]!=''){ $draw_res=$result['team_drawn'][$result['team_list'][$i]]; } else { $draw_res=0; } 调用将数字数组转换为字符串数组,let string = array.map { String($0) } .joined(separator: ", ") 将它们组合成一个字符串,并在各个字符串之间使用任何分隔符。

或者,如果要在UI中显示这些数字可能需要小数点和/或数千个分隔符,那么最好使用map以本地化格式显示结果:< / p>

joined

对于美国用户而言,这将导致:

  

1.00,1.10,1.20,1.30

但是对于德国用户来说,它会导致:

  

1,00,1,10,1,20,1,30

答案 1 :(得分:2)

您只需要将整数映射到字符串中,然后将它们与分隔符Dim bricks() As PictureBox Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load bricks = BrickPanel.Controls. OfType(Of PictureBox)(). OrderBy(Function(pb) pb.Name). 'Naive OrderBy... 10 is before 2. I leave it to the OP to fix that part ToArray() End Sub

连接起来
 Dim bricks(54) As PictureBox

 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim i As Integer = 0
    For r As Integer = 1 To 10
         For c As Integer = 1 to 11 - r
             bricks(i) = Me.Controls("Row" & r & "_Brick" & c)
             i+=1
         Next c
    Next r
End Sub