I'm making a word counter desktop application in ASP.NET C#. For making this I'm using:
=> String str = txt_box.Text; => Char[] space={' '}; => int word_count = str.Split(space,StringSplitOption.RemoveEmptyEnteries).Length; => MessageBox.show(" Number of words = " + word_count);
The program calculates words properly, but when I press the Enter key, it does not count the word which is entered just after pressing the Enter key.
Example:
1)Hi, my name is Satpreet Singh
2)I'm a .NET Developer
Output: Number of words = 9
Explanation:
In this output, (Actual is 10 but it's showing 9)
When I press the Enter Key after typing the word "Singh" it can't split. It was merged with the word "I'm".
I have no experience in ASP.NET, but you could try the following, Make a string, use LINQ to remove all non letter characters, split the string on all the whitespaces into an array, print the length of the array
string str = "Hi, my name is Satpreet Singh\nI'm a .NET Developer";
str = new string((from c in str
where char.IsWhiteSpace(c) || char.IsLetterOrDigit(c)
select c
).ToArray());
string[] splitted = str.Split(new char[0]);
Console.WriteLine(splitted.Length);
Console.ReadLine();
Output: 10
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.