如何从数组中的字符串中检索值 [英] how to retrieve value from arraylist in string

查看:67
本文介绍了如何从数组中的字符串中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在工作asp.net项目.我上课了,它的返回类型是arraylist,而类返回带有多个值的arrylist,现在我想使用arrylist中的所有值,所以
我以邮件形式发送邮件我希望所有数值都在一个建议中建议我


i m working asp.net project. i had taken class its return type is arraylist and class return arrylist with multiple values now i want use all values from arrylist so
i send mail in mail i want all value in single pls suggest me

推荐答案

仅连接值,例如
The just concatenate the values, e.g.
ArrayList al = new ArrayList();
al.Add(5);
al.Add("foo");
al.Add(true);
string s="";
foreach (object o in al)
{
  s += o.ToString();
}



如果您有很多项目,请考虑使用 StringBuilder [



If you have many items please consider using the StringBuilder[^] class.


http://msdn.microsoft.com上的


好示例/en-us/library/ttw7t8t6(v=vs.80).aspx [
nice example on http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.80).aspx[^]




您可以在互联网上找到针对您案件的通用解决方案.

这是我发现的:
Hi,

You can find generic solution for your case on internet.

Here is what I found:
public static class ExtensionMethods 
{     
	// for generic interface IEnumerable<t>     
	public static string ToString<t>(this IEnumerable<t> source, string separator)     
	{         
		if (source == null)             
			throw new ArgumentException("Parameter source can not be null.");         

		if (string.IsNullOrEmpty(separator))             
			throw new ArgumentException("Parameter separator can not be null or empty.");         

		string[] array = source.Where(n => n != null).Select(n => n.ToString()).ToArray();         
		return string.Join(separator, array);
	}     

	// for interface IEnumerable     
	public static string ToString(this IEnumerable source, string separator)     
	{         
		if (source == null)             
			throw new ArgumentException("Parameter source can not be null.");         

		if (string.IsNullOrEmpty(separator))             
			throw new ArgumentException("Parameter separator can not be null or empty.");

		string[] array = source.Cast<object>().Where(n => n != null).Select(n => n.ToString()).ToArray();
		return string.Join(separator, array);    
	} 
}
</t></t></t>


使用此扩展方法的示例:


Example of using this extension method:

ArrayList myArrayList = new ArrayList();
myArrayList.Add("Item1");
myArrayList.Add("Item2");
myArrayList.Add("Item3");
myArrayList.Add("Item4");
myArrayList.Add("Item5");
	
string myListAsString = myArrayList.ToString("; ");



输出:
Item1;项目2;项目3;项目4; Item5



Output:
Item1; Item2; Item3; Item4; Item5


这篇关于如何从数组中的字符串中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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