这是我所拥有的。我的想法是将字符串填充到数组中,对其进行格式化并将其重新转换为字符串。
//input search term
Console.WriteLine("What is your search query?:");
string searchTerm = Console.ReadLine();
//stuff the search term into an array to split it out
string separator = " "; //assumes search terms are separated by spaces
string[] searchTermArray = searchTerm.Split(separator.ToCharArray());
//construct the search term
string searchTermFormat = "";
for (int i = 0; i < searchTermArray.Length; i++)
{
searchTermFormat += searchTermArray[i] + "+";
//Console.WriteLine(searchTermFormat);
}
期望的输出
word1+word2+word3
其中单词数量不固定。
答案 0 :(得分:7)
String.Join("+", searchTermArray)
searchTerm.Replace(' ', '+')
Uri.EscapeDataString(searchTerm)
答案 1 :(得分:1)
使用String.Join
将字符串连接在一起。
答案 2 :(得分:0)
尝试string.Replace(" ", "+")
答案 3 :(得分:0)
典型的String.Replace作业或RegEx
答案 4 :(得分:0)
myString.Replace('','+');
怎么样?