本文介绍了为什么总是在结尾处将空字符串[非空白]添加到字符串变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ex,
字符串str ="Hai";
逐行执行并检查str持有什么(选择整个内容(cntl + A),然后在字母i后面查看)
ex,
string str = "Hai";
Do line by line execution and check what str holds(Select the whole thing (cntl + A) and See after the letter i)
推荐答案
string str = "aaa";
//Test 1
if (str.Contains(String.Empty))
MessageBox.Show("String aaa contains an empty string");
//Test 2
int pos = str.IndexOf(String.Empty);
int pos2 = str.IndexOf("a");
//Feedback
MessageBox.Show("String.Empty at Position: " + pos.ToString());
MessageBox.Show("First 'a' is at Position: " + pos.ToString());
MessageBox.Show("The 2 position index match: " + (pos == pos2));
string str = "aaa";
str += "*";
string[] Ss = str.Split(new char[]{'*'}, StringSplitOptions.RemoveEmptyEntries);
int a = Ss.Length;
Response.Write(a);
if(Ss[1] == string.Empty) Response.Write("Empty string is there....");
当然,由于Ss只有1个元素长,因此最后一行现在将引发异常.
Of course, the last line will now throw an exception as Ss is only 1 element long.
这篇关于为什么总是在结尾处将空字符串[非空白]添加到字符串变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!