c#将队列中所有元素构成一个字符串返回?

可以使用C#的String.Join方法来将队列中所有元素构成一个字符串返回,示例代码如下:

using System;
using System.Collections.Generic;

class Program {
    
    
    static void Main(string[] args) {
    
    
        Queue<string> queue = new Queue<string>();
        queue.Enqueue("hello");
        queue.Enqueue("world");
        queue.Enqueue("!");

        string result = String.Join("", queue.ToArray());
        Console.WriteLine(result); //输出helloworld!
    }
}

这里将队列转换成数组,然后使用String.Join方法将数组中的元素组合成一个字符串,拼接时用空字符串作为分隔符。最终得到的结果是"helloworld!"。

猜你喜欢

转载自blog.csdn.net/2302_77270563/article/details/129967417