如何使用子字符串

编程入门 行业动态 更新时间:2024-10-26 22:28:54
本文介绍了如何使用子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我正在尝试从数据集中获取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 for substring:

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));

更多推荐

如何使用子字符串

本文发布于:2023-10-26 22:07:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何使用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!