简体   繁体   English

如何在C#中将单个空格放入字符串中

[英]How can you put single spaces into a string of characters in C#

I am still a beginner in C# and I know there is a method that can be used to do this but I can't seem to find it online. 我仍然是C#的初学者,我知道可以使用一种方法来执行此操作,但是我似乎无法在网上找到它。

I have a function that permutates a word 我有一个排列单词的功能

static void Main(string[] args)
{
    string[] list = "a b c d".Split();
    foreach (string[] permutation in Permutations<string>.AllFor(list))
    {
        System.Console.WriteLine(string.Join(" ", permutation));
    }
}

However it only works with words that are broken up. 但是,它仅适用于分解的单词。 (eg. "abcd" ) Since that is not really a practical way to ask a user for input, I want to find a way to take a word from the user (an unbroken word like "hello" ) and break it up for the function to understand. (例如“ abcd”),因为这实际上并不是要求用户输入的实际方法,所以我想找到一种方法来从用户那里拿一个单词(一个完整的单词,例如“ hello”)并分解为功能了解。 Eg. 例如。 form the input word of the use "happy" to a spaced word for the program to understand = "happy" 将“ happy”的输入词形成一个空格,以使程序理解为“ happy”

I tried this code: 我尝试了这段代码:

//splits the word into an array
string[] arr = name.Split();

//splits the array with spaces to enter into the program
name = string.Join(" ",arr);

arr = name.Split();

But it just ends up coming out unbroken anyway. 但是无论如何它最终都不会中断。 Can someone tell me the easiest way to do this? 有人可以告诉我最简单的方法吗?

Just to mention I am still a beginner in C# and programming in total I might not understand some of the higher level concepts. 只需提一下,我仍然是C#和编程的初学者,总的来说,我可能不理解某些高级概念。 I have been through some answers on this website and I have seen some answers that I don't understand at all. 我已经在该网站上浏览了一些答案,并且看到了一些我根本不理解的答案。

You just need to separate each character and then concatenate them with a space: 您只需要分隔每个字符,然后用空格将它们连接起来即可:

This is the simplest way: 这是最简单的方法:

var userInput = Console.ReadLine();

var output = string.Join<char>(" ", userInput);

Console.WriteLine(output);

You can loop over the string to convert it to an array, and then use Join . 您可以遍历字符串以将其转换为数组,然后使用Join

using System.Text.RegularExpressions;
using System;

public class Program{
    public static void Main(string[] args) {
        string v = "hello";

        // Convert into the a string array, the old-fashioned way.
        string[] name = new string[v.Length];
        for (int i = 0; i < v.Length; i++)
        name[i] = v[i] + ""; 


        string feedToPermutationFunction = string.Join(" ",name));
        // Feed the above string into your permutation code.
    }
}
    char[] array=input.ToArray();
string val="";
for(int i=0;i<array.Length;i++)
{
val+=array[i]+" ";
}

this will give you a string with spaces like you wanted Val create an array with the string length 这将为您提供一个带有空格的字符串,就像您想要的Val那样用字符串长度创建一个数组

string[] strarray=new string[val.Length];


   for(int i=0;i<strarray.Length;i++)
{
strarray[i]=val.Substring(i,len); //**i** is for string index,,,**len** string length in each index
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM