计算空间并存储在字符串数组中 [英] Count space and stored in string array

查看:68
本文介绍了计算空间并存储在字符串数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不会接受字符串我的名字是rohit

和字符串中的空格数。

并且用户不会给出一个iedex并在a之后打印一个单词那个空间。



===============================



Ex。

输入字符串。

我的名字是rohit



输入索引编号

2



您的搜索是......是



==== =========================

Plz任何可以帮助我.......

或建议我该怎么做....?

和怎么办....?



Thnx in提前.........



我尝试过:



class Program

{

static void Main()

{

string [] w = SplitWords(rohit vijay batho nagpur);

foreach(字符串s in w)

{

Console.WriteLine(s); < br $>
}



// Console.WriteLine(输入索引号);

// w = Console.ReadLine();



Console.ReadLine();

}



///< summary>

///取出所有单词输入字符串并将它们分开。

///

静态字符串[] SplitWords(字符串s)

{

//

//拆分所有非单词字符。

// ...返回所有单词的数组。

//

返回Regex.Split(s,@\ W +);





// @特殊逐字字符串语法

// \ W +一个或多个非单词字符

}

}

解决方案

1。按空格将字符串拆分为单词数组, [ ^ ]

2.空格数将是此数组的计数减去1, [ ^ ]

3.要获得数组中索引编号i的项目,只需myArray [i], [ ^ ]


尝试下面的代码: -



  string  inputString =  我的名字是rohit; 
string [] wordArray = inputString.Split( null ); // 按空格分割字符串。

// 算上一个。空白
int countOfSpace = wordArray.Length -1;

int index = 2 ;
string newString = inputString.Substring(index);
string wordAfterIndex = newString.Split( null )[ 1 ]; // 指定您希望从数组中找到哪个单词的索引。


I wont to take string "My name is rohit"
and Count No of Spaces in String.
and wont user gives a iedex and print a word after a that space.

===============================

Ex.
Enter String .
My name is rohit

Enter Index no.
2

Your Search is...... " is"

=============================
Plz Any can Help me .......
or Suggest me what can i do....?
and How to do....?

Thnx in advance.........

What I have tried:

class Program
{
static void Main()
{
string[] w = SplitWords("rohit vijay batho nagpur");
foreach (string s in w)
{
Console.WriteLine(s);
}

// Console.WriteLine("Enter Index No.");
// w = Console.ReadLine();

Console.ReadLine();
}

/// <summary>
/// Take all the words in the input string and separate them.
///
static string[] SplitWords(string s)
{
//
// Split on all non-word characters.
// ... Returns an array of all the words.
//
return Regex.Split(s, @"\W+");


// @ special verbatim string syntax
// \W+ one or more non-word characters together
}
}

解决方案

1. Split the string into array of words by space, [^]
2. The number of space will be the count of this array minus one, [^]
3. To get the item at index number i in the array, simply myArray[i], [^]


Try Below code:-

string inputString = "My name is rohit";
string[] wordArray = inputString.Split(null); //split the string by whitespace.

//count the no. of whitespace
int countOfSpace = wordArray.Length -1;

int index = 2;
string newString = inputString.Substring(index);
string wordAfterIndex = newString.Split(null)[1]; //specify the index as to which word you want from the array.


这篇关于计算空间并存储在字符串数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆