获取字符串变量的最后一个值 [英] get last value of string variable
本文介绍了获取字符串变量的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我定义string [] name = new string [100];
当我填写此变量时,我现在不知道如何访问最后一个值???它已更改,有时78有时50,有时60,...
我怎么能得到最后一个值???
解决方案
访问这里...
http://stackoverflow.com/questions/7387396/get-the-first-and-last-item-of-an-array-of-strings [< a href =http://stackoverflow.com/questions/7387396/get-the-first-and-last-item-of-an-array-of-stringstarget =_ blanktitle =New Window> ^ ]
或
>
也许会这样:
public int LastNonEmptyIndex( string [] array)
{
int i = array.Length - 1 ;
do {
if (!string.IsNullOrEmpty(array [i]) ){
break ;
}
} while ( - i > -1);
return i;
}
此函数返回包含非emty / null值的数组的最后一个索引。
希望这会有所帮助。
首先,请阅读我对该问题的评论。
解决方案2 by Rakesh Meel非常好,我建议你通过研究这些文章来提高你的知识:
阵列教程 [ ^ ]
数组(C#编程指南) [ ^ ]
代码:初始化数组(Visual C#) [ ^ ]
i define string[] name=new string[100];
when i fill this variable,i don't now how i can access to last value???it's changed,sometime 78 sometime 50 sometime 60,...
how i can get last value???
解决方案
visit Here...
http://stackoverflow.com/questions/7387396/get-the-first-and-last-item-of-an-array-of-strings[^]
or
http://stackoverflow.com/questions/1056749/finding-the-last-index-of-an-array
Maybe that would do:
public int LastNonEmptyIndex(string[] array) { int i = array.Length - 1; do { if (!string.IsNullOrEmpty(array[i])) { break; } } while (--i > -1); return i; }
This function returns the last index of an array containing a non emty/null value.
Hope this helps.
First of all, please, read my comment to the question.
Solution 2 by Rakesh Meel is very good and i would suggest you to improve your knowledge by studying these articles:
Arrays Tutorial[^]
Arrays (C# Programming Guide)[^]
Code: Initializing an Array (Visual C#)[^]
这篇关于获取字符串变量的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文