如何将数据限制为前5000个字符 [英] How do I limit the data to the first 5000 characters

查看:129
本文介绍了如何将数据限制为前5000个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据限制为前5000个字符。



I want to limit the data to the first 5000 characters.

PostData = TextBox2.Text.Substring(0, 5000)





出错:





Getting an Error:

"Index and length must refer to a location within the string.
Parameter name: length "

推荐答案

当textbox2值的长度小于5000时会发生这种情况,试试这个:

This happens when the length of textbox2 value is less than 5000, try this:
String PostData = TextBox2.Text;
if (PostData.Length > 5000){
    PostData = PostData.Substring(0,5000);
}


或者它可能在一行中:

or it could be like this in one line :
string PostData = (TextBox2.Text.Length > 5000 ? TextBox2.Text.Substring(0, 5000) : TextBox2.Text);


这篇关于如何将数据限制为前5000个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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