- I want to set the limit min. 5 characters per line and
- 我想设置每行5个字符的限制
- I want to show message if
multiline
textbox
have less than 5chars
. line. - 如果多行文本框的字符数小于5,我想显示消息。线。
Here is my code.
这是我的代码。
string[] temp = Regex.Split(KeywordList.Text, "\r\n");
for (int i = 0; i < temp.Length; i++)
{
if (temp[i].Length <= 4) //check characters limit
{
Response.Write("Please enter min 5 characters per line");
}
else
{
Response.Write("success");
}
}
My textbox contains min. 5 characters in few lines but above code show "success".
我的文本框在几行中包含5个字符,但上面的代码显示“成功”。
Can anyone help me to detect less than 5 characters row.
谁能帮我检测不到5个字符的行吗?
Thanks in advance, sorry for my bad English.
提前谢谢,对不起,我的英语不好。
1 个解决方案
#1
2
I suggest using Linq: Split
the initial string (please, do not put magic values like "\r\n"
, but Environment.NewLine
) and check if Any
item is 4
characters or less:
我建议使用Linq:分割初始字符串(请不要使用像“\r\n”这样的魔法值,而是使用Environment.NewLine),并检查任何项是否为4个字符或更少:
if (KeywordList
.Text
.Split(new string[] {Environment.NewLine}, StringSplitOptions.None)
.Any(item => item.Length <= 4))
Response.Write("Please enter min 5 characters per line");
else
Response.Write("success");
In your current code Regex.Split
is an overshoot (String.Split
is enough), and it seems that you should add break
after Response.Write("Please enter min 5 characters per line");
line:
在当前代码Regex中。分割是超调(字符串)。拆分是足够的),并且似乎您应该在响应之后添加break。写(“请每行输入5个字符”);线:
...
if (temp[i].Length <= 4) //check characters limit
{
Response.Write("Please enter min 5 characters per line");
break; // <- we have an error, no need to check more lines
}
...
Edit: If you are not sure in delimiters (they can be \n
- Unix, \r\n
- Windows, \n\r
- Mac etc.) you can try several ones in one call
编辑:如果您不确定在定界器(它们可以是\n - Unix、\r - \n - Windows、\n - \n - r - Mac等),您可以在一次调用中尝试几个
if (KeywordList
.Text
.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries)
.Any(item => item.Length <= 4))
Response.Write("Please enter min 5 characters per line");
else
Response.Write("success");
However, we have to eliminate empty items: for "a\r\n\b"
we want ["a", "b"]
, not ["a", "", "b"]
然而,我们必须消除空项目:对于“a\r\n\b”,我们想要“a”、“b”,而不是“a”、“b”
#1
2
I suggest using Linq: Split
the initial string (please, do not put magic values like "\r\n"
, but Environment.NewLine
) and check if Any
item is 4
characters or less:
我建议使用Linq:分割初始字符串(请不要使用像“\r\n”这样的魔法值,而是使用Environment.NewLine),并检查任何项是否为4个字符或更少:
if (KeywordList
.Text
.Split(new string[] {Environment.NewLine}, StringSplitOptions.None)
.Any(item => item.Length <= 4))
Response.Write("Please enter min 5 characters per line");
else
Response.Write("success");
In your current code Regex.Split
is an overshoot (String.Split
is enough), and it seems that you should add break
after Response.Write("Please enter min 5 characters per line");
line:
在当前代码Regex中。分割是超调(字符串)。拆分是足够的),并且似乎您应该在响应之后添加break。写(“请每行输入5个字符”);线:
...
if (temp[i].Length <= 4) //check characters limit
{
Response.Write("Please enter min 5 characters per line");
break; // <- we have an error, no need to check more lines
}
...
Edit: If you are not sure in delimiters (they can be \n
- Unix, \r\n
- Windows, \n\r
- Mac etc.) you can try several ones in one call
编辑:如果您不确定在定界器(它们可以是\n - Unix、\r - \n - Windows、\n - \n - r - Mac等),您可以在一次调用中尝试几个
if (KeywordList
.Text
.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries)
.Any(item => item.Length <= 4))
Response.Write("Please enter min 5 characters per line");
else
Response.Write("success");
However, we have to eliminate empty items: for "a\r\n\b"
we want ["a", "b"]
, not ["a", "", "b"]
然而,我们必须消除空项目:对于“a\r\n\b”,我们想要“a”、“b”,而不是“a”、“b”