C# 如何将字符串转换为字符串[]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11081549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:24:25  来源:igfitidea点击:

How to convert string to string[]?

c#

提问by Mihir

How to convert stringtype to string[]type in C#?

如何将string类型转换为string[]C# 中的类型?

采纳答案by Carsten

string[]is an array (vector) of strings stringis just a string (a list/array of characters)

string[]是字符串数组(向量) string只是字符串(字符列表/数组)

Depending on how you want to convert this, the canonical answer could be:

根据您想如何转换它,规范的答案可能是:

string[] -> string

字符串[] -> 字符串

return String.Join(" ", myStringArray);

string -> string[]

字符串 -> 字符串[]

return new []{ myString };

回答by zerkms

stringis a string, and string[]is an array of strings

string是一个字符串,并且string[]是一个字符串数组

回答by Simeon Visser

A stringis one string, a string[]is a string array. It means it's a variable with multiple strings in it.

Astring是一个字符串,astring[]是一个字符串数组。这意味着它是一个包含多个字符串的变量。

Although you can convert a stringto a string[](create a string array with one element in it), it's probably a sign that you're trying to do something which you shouldn't do.

尽管您可以将 a 转换string为 a string[](创建一个包含一个元素的字符串数组),但这可能表明您正在尝试做一些不应该做的事情。

回答by ericosg

A string holds one value, but a string[] holds many strings, as it's an array of string.

一个字符串保存一个值,但一个 string[] 保存许多字符串,因为它是一个字符串数组。

See more here

在这里查看更多

回答by ?yvind Br?then

zerkms told you the difference. If you like you can "convert" a string to an array of strings with length of 1.

zerkms 告诉你区别。如果您愿意,可以将字符串“转换”为长度为 1 的字符串数组。

If you want to send the string as a argument for example you can do like this:

例如,如果您想将字符串作为参数发送,您可以这样做:

var myString = "Test";

MethodThatRequiresStringArrayAsParameter( new[]{myString} );

I honestly can't see any other reason of doing the conversion than to satisty a method argument, but if it's another reason you will have to provide some information as to what you are trying to accomplish since there is probably a better solution.

老实说,除了满足方法参数之外,我看不出任何其他原因进行转换,但是如果这是另一个原因,您将不得不提供一些有关您要完成的任务的信息,因为可能有更好的解决方案。

回答by Tim Schmelter

An array is a fixed collection of same-type data that are stored contiguously and that are accessible by an index (zero based).

数组是相同类型数据的固定集合,这些数据连续存储并可通过索引(从零开始)访问。

A string is a sequence of characters.

字符串是一个字符序列。

Hence a String[]is a collection of Strings.

因此 aString[]是 的集合Strings

For example:

例如:

String foo = "Foo";  // one instance of String
String[] foos = new String[] { "Foo1", "Foo2", "Foo3" };
String firstFoo = foos[0];  // "Foo1"

Arrays (C# Programming Guide)

数组(C# 编程指南)

Edit: So obviously there's no direct way to convert a single Stringto an String[]or vice-versa. Though you can use String.Splitto get a String[]from a Stringby using a separator(for example comma).

编辑:很明显,没有直接的方法可以将单个转换为一个StringString[]反之亦然。尽管您可以使用分隔符(例如逗号)从 a 中String.Split获取 a 。String[]String

To "convert" a String[]to a String(the opposite) you can use String.Join. You need to specify how you want to join those strings(f.e. with comma).

要将 a 转换String[]为 a String(相反),您可以使用String.Join. 您需要指定如何加入这些字符串(fe 用逗号)。

Here's an example:

下面是一个例子:

var foos = "Foo1,Foo2,Foo3";
var fooArray = foos.Split(',');  // now you have an array of 3 strings
foos = String.Join(",", fooArray); // now you have the same as in the first line

回答by Klaus Byskov Pedersen

You can create a string[](string array) that contains your stringlike :

您可以创建一个string[]包含您string喜欢的(字符串数组):

string someString = "something";
string[] stringArray = new string[]{ someString };

The variable stringArraywill now have a length of 1 and contain someString.

该变量stringArray现在的长度为 1 并包含someString.

回答by Stig-Rune S.

In case you are just a beginner and want to split a string into an array of letters but didn't know the right terminology for that is a char[];

如果你只是一个初学者,想把一个字符串拆分成一个字母数组,但不知道正确的术语是 char[];

String myString = "My String";
char[] characters = myString.ToCharArray();

If this is not what you were looking for, sorry for wasting your time :P

如果这不是您要找的东西,很抱歉浪费您的时间:P

回答by regisbsb

Casting can also help converting string to string[]. In this case, casting the string with ToArray() is demonstrated:

转换还可以帮助将字符串转换为字符串 []。在这种情况下,演示了使用 ToArray() 转换字符串:

String myString = "My String";
String[] myString.Cast<char>().Cast<string>().ToArray();

回答by Sayed Abolfazl Fatemi

If you want to convert a string like "Mohammad"to String[]that contains all characters as String, this may help you:

如果你想将字符串转换一样"Mohammad",以String[]包含所有字符String,这可以帮助你:

"Mohammad".ToCharArray().Select(c => c.ToString()).ToArray()