c# List<JToken>转List<string>
时间: 2023-12-03 08:02:29 浏览: 44
可以使用LINQ进行转换,示例代码如下:
```csharp
List<JToken> jTokenList = new List<JToken>();
// 假设已经给 jTokenList 添加了元素
List<string> stringList = jTokenList.Select(token => token.ToString()).ToList();
```
其中,`Select()` 方法将 `jTokenList` 中的每个 `JToken` 对象转换为对应的字符串,最终通过 `ToList()` 方法转换为 `List<string>` 类型的集合。
相关问题
c# list<string>转string
以下是两种C#中List<string>转string[]的方法:
1.使用List<string>的ToArray()方法
```csharp
List<string> listS = new List<string>();
listS.Add("str");
listS.Add("hello");
string[] str = listS.ToArray();
```
2.使用LINQ的ToArray()方法
```csharp
List<string> listS = new List<string>();
listS.Add("str");
listS.Add("hello");
string[] str = listS.ToArray<string>();
```
c# list<string>转换为byte[]
你可以使用 `System.Text.Encoding` 类的 `GetBytes` 方法将 `List<string>` 转换为 `byte[]`。以下是一个示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main()
{
List<string> stringList = new List<string>
{
"Hello",
"World",
"C#"
};
// 将 List<string> 转换为 string,并使用换行符连接所有字符串
string combinedString = string.Join(Environment.NewLine, stringList);
// 将 string 转换为 byte[]
byte[] bytes = Encoding.UTF8.GetBytes(combinedString);
Console.WriteLine("转换后的 byte 数组:");
foreach (byte b in bytes)
{
Console.Write(b + " ");
}
}
}
```
运行上述代码,将会输出 List<string> 转换后的 byte 数组。请注意,此示例中使用了 UTF-8 编码进行转换,你也可以根据需要选择其他编码方式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)