字符串加入列表和项目

时间:2017-02-24 08:04:53

标签: c# string

我需要加入字符串和List<string>项。我的代码

List<string> list = new List<string>() { "item1", "item2" };
string item3 = "item3";

string result = string.Join(",", list, item3); 

结果是

//System.Collections.Generic.List`1[System.String],item3

而非预期

//item1,item2,item3

最优雅的方法是什么,以解决这个内联?可能有多个List<string>和多个string,每个<Grid > <Grid.ColumnDefinitions> <ColumnDefinition Width="5*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="7*"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <CheckBox x:Name="cbMoveResize" Content="Move and resize" Grid.Row="0"/> <CheckBox x:Name="cbDrawRectangle" Content="Draw rectangle" Grid.Row="0"/> </StackPanel> <ScrollViewer Grid.Column="0" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Auto"> <Grid x:Name="mainGrid" Grid.Column="0" Background="Gray" Width="Auto" Height="Auto" MouseMove="Image_MouseMove" MouseDown="Image_MouseDown" MouseUp="Image_MouseUp" MouseLeave="leaveGrid" MouseWheel="UIElement_OnMouseWheel"> <Image x:Name="bmpImage" HorizontalAlignment="Left" VerticalAlignment="Top" /> <Canvas IsHitTestVisible="True" x:Name="imageCanvas" Width="{Binding Width, ElementName=bmpImage}" Height="{Binding Height, ElementName=bmpImage}"> <ItemsControl ItemsSource="{Binding rectTest, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas IsItemsHost="True"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style TargetType="ContentPresenter"> <Setter Property="Canvas.Left" Value="{Binding Path = r.X}"/> <Setter Property="Canvas.Top" Value="{Binding Path = r.Y}"/> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemTemplate> <DataTemplate> <ContentControl Height="{Binding r.Height, Mode=TwoWay}" Width="{Binding r.Width, Mode=TwoWay}" x:Name="nume"> <ContentControl.Style> <Style TargetType="ContentControl"> <Setter Property="Template" Value="{StaticResource defaultTemplate}"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=cbMoveResize, Path=IsChecked}" Value="True"> <Setter Property="Template" Value="{StaticResource moveAndResizeTemplate}"/> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> <Rectangle Stroke="Red" StrokeThickness="2" Height="{Binding r.Height, Mode=TwoWay}" Width="{Binding r.Width, Mode=TwoWay}"/> </ContentControl> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Canvas> </Grid> </ScrollViewer> <Grid Name="smallGrid" Grid.Column="2" Grid.Row="2"> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="8*"/> </Grid.RowDefinitions> <Button Name="btnLoad" Margin="2" Height="25" Content="Load" VerticalAlignment="Top" Click="btnLoad_Click"/> <Canvas x:Name="grid2" Grid.Row="1" Width="{Binding Width, ElementName=smallGrid}" Height="Auto"> <Image x:Name="cropIm" Width="{Binding ActualWidth, ElementName=grid2, Mode=OneWay}" Height="{Binding ActualHeight, ElementName=grid2, Mode=OneWay}" /> </Canvas> </Grid> </Grid> 都可以为空。

3 个答案:

答案 0 :(得分:7)

您可以使用Concat连接所有字符串(不更改原始列表!)和可枚举的Join

List<string> list = new List<string>() { "item1", "item2" };
string item3 = "item3";

string result = string.Join(",", list.Concat(new string[] { item3 }));
// result = item1,item2,item3

您当前代码的问题在于它调用string.Join(string, params IEnumerable<object>):它会将list视为对象,而不是对象的lis。

答案 1 :(得分:1)

如果最终结果需要是单个string,那么您可以使用类似的内容。这将首先将list加入单string,然后将其加入item3

string result = string.Join (",", string.Join (",", list), item3);

对于每个额外的List,您可以添加其他string.Join (",", otherList)子句。

答案 2 :(得分:0)

那怎么样?

string result = string.Join(",", list.Concat(new string[1] { item3 }));