简体   繁体   English

如何获取不同数组中的拆分字符串

[英]How to get Split string in different array

I want to get input from user and if find space put the first string in array and second string in another new array such as when the user input is first second third i want to save first in char array and second in another char array. 我想从用户那里获取输入,如发现space把第一个字符串数组和第二个字符串中另一个新的阵列,例如当用户输入是first second third我要救first字符数组和second在另一个字符数组。 I write this code to split but don't know how to save input in array and the number of arrays such as number of spaces. 我编写此代码是为了拆分,但不知道如何在数组中保存输入以及数组的数量(例如空格数)。

s = Console.ReadLine();
string[] ssize = s.Split(null);

A string can be accessed as if it is a char array so if you do the following: 可以像访问char数组一样访问字符串,因此如果执行以下操作:

var s = "one two three";
string[] ssize = s.Split(' ');
var myChar = ssize[0][0];

then myChar will be o ; myChar将为o ;

Very simple: 很简单:

var s = "first second third";
var words = s.Split(' ');
var charArrays = words.ToList().ConvertAll(x => x.ToCharArray());

您不需要将字符串存储在char数组中:只需在需要获取char数组时对ssize数组中的每个字符串调用ToCharArray。

Just use 只需使用

string[] ssize = s.Split(' ');

The number of words entered is given by 输入的单词数为

ssize.Length

To access single characters you can use the indexing operator of the strings directly: 要访问单个字符,可以直接使用字符串的索引运算符:

char firstCharOfFirstWord = sstring[0][0];

You are doing in the right way already. 您已经在以正确的方式进行操作。

s = Console.ReadLine();
//User inputs "First Second Third"
string[] ssize = s.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries);
foreach(string str in ssize)
   Console.WriteLine("string: {0}", str);

or 要么

s = Console.ReadLine();
//User inputs "First Second Third"
string[] ssize = s.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < ssize .Length; i++)
   Console.WriteLine("string {0}: {1}", i+1, ssize [i]);

ssize is an array and each element will contain one string. ssize是一个数组,每个元素将包含一个字符串。

See this: http://msdn.microsoft.com/en-us/library/tabh47cf%28v=vs.110%29.aspx 看到这个: http : //msdn.microsoft.com/en-us/library/tabh47cf%28v=vs.110%29.aspx

s.Split(' ').Select(p=>p.ToCharArray()).ToArray();

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

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