C#检查字长

时间:2015-04-30 07:57:58

标签: c#

如何检查字符串中每个单词的长度,如果一个大于10,则显示不同的响应?

Semi Psuedo

string allwords= "This is a test this is a test aaaaaaaaaaa this is a test";

if (allwords.Length < 10) {
      Console.WriteLine (allwords.Length);

}
else
{
   Console.WriteLine ("Woahh there one of these words is more than 10 chars");
}

2 个答案:

答案 0 :(得分:7)

您可以执行以下操作:

allWords.Split(' ').Any(w => w.Length > 10);

答案 1 :(得分:0)

你可以用4种不同的方式做到这一点。 只记得添加

select date_part('day', "toDate" - "fromDate") from "Reservation" where "reservationID" = 1;

您的项目的命名空间。

第一种方式:使用Any方法和if语句

using System;
using System.Linq;

第二种方式:使用Any方法和?操作

string allwords = "This is a test this is a test aaaaaaaaaaa this is a test ";
bool c = allwords.Split().Any(s => s.Length > 10);
if (c == false)
    Console.WriteLine(allwords);
else
    Console.WriteLine("Woahh there one of these words is more than 10 chars");

第三种方式:使用string []和foreach以及if语句

string allwords = "This is a test this is a test aaaaaaaaaaa this is a test ";
bool c = allwords.Split().Any(s => s.Length > 10);
Console.WriteLine(c == false ? allwords : "Woahh there one of these words is more than 10 chars");

第四种方式:使用string []和foreach和?操作

string allwords = "This is a test this is a test aaaaaaaaaaa this is a test ";
string[] c = allwords.Split();
bool moreThanten = false;
foreach (string v in c)
    if (v.Length > 10)
        moreThanten = true;
if (moreThanten == false)
    Console.WriteLine(allwords);
else
    Console.WriteLine("Woahh there one of these words is more than 10 chars");