如何使用子字符串 [英] How to use substring
问题描述
您好,
我正在尝试从数据集中获取intime值,我得到的是字符串格式,如'01 / 01/1900 10:00:00 AM'
txtintime.Text =(ds.Tables [0] .Rows [0] [5] .ToString()。Substring(11,11));
输出:txtintime.Text = 10:00:00 AM;
它运行正常但是当我在IIS上运行它时会显示索引超出范围的错误。 br />
那我该如何处理这个异常。
Plz回复,
谢谢你
substring
的语法:
string string .substring( int startIndex, int length)
试用:
子串( 12 , 11 )
或者,如果你知道字符串将永远就像
约会时间AM
更好的方法是使用split
string the_string = 01/01/1900 10:00:00 AM;
string [] tmp_split = the_string.Split(' ');
MessageBox.Show(tmp_split [ 1 ]);
// 输出
// tmp_split [0] = 01/01/1900
// tmp_split [1] = 10:00:00
// tmp_split [2] = AM
你不必计算任何长度。只需从数组中获取它:)
请仅使用格式日期时间并获得预期结果:
DateTime abc = DateTime.Now;
Label3.Text = abc.ToString( MM / DD / YYYY hh:mm:ss tt).Substring( 11 , 11 );
此代码将帮助您始终获得预期结果
txtintime .Text =(ds.Tables [ 0 ]。行[ 0 ] [ 5 ]。ToString( MM / DD / YYYY hh:mm:ss tt )。子串( 11 , 11 ));
Hello,
I'm trying to fetch intime value from dataset and i got it in string format as like '01/01/1900 10:00:00 AM'
txtintime.Text = (ds.Tables[0].Rows[0][5].ToString().Substring(11, 11));
Output: txtintime.Text = 10:00:00 AM;
It works fine but when i run it on IIS it shows error of index out of range.
So how can i handle this exception.
Plz reply,
Thank you
Syntax forsubstring
:
string string.substring(int startIndex,int length)
Try with:
Substring(12, 11)
Or, if you know that the string will always be like
"date time AM"
the better way would be to use split
string the_string = "01/01/1900 10:00:00 AM"; string[] tmp_split = the_string.Split(' '); MessageBox.Show(tmp_split[1]); //the output //tmp_split[0] = 01/01/1900 //tmp_split[1] = 10:00:00 //tmp_split[2] = AM
And you dont have to count any length. Just get it from the array :)
Please use format datetime only and get your expected result:
DateTime abc = DateTime.Now; Label3.Text = abc.ToString("MM/DD/YYYY hh:mm:ss tt").Substring(11,11);
This code will help you are alway get the your expected result
txtintime.Text = (ds.Tables[0].Rows[0][5].ToString("MM/DD/YYYY hh:mm:ss tt").Substring(11, 11));
这篇关于如何使用子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!