StringCollection 类表示字符串的集合。以下是 StringCollection 类的属性 -
属性及说明 | |
---|---|
1 | Count |
2 | IsReadOnly |
3 | IsSynchronized strong> |
4 |
Item[Int32] 获取或设置指定索引处的元素。 |
5 |
SyncRoot 获取可用于同步对 StringCollection 的访问的对象。 |
以下是 StringCollection 类的方法 -
老师编号 | 方法及说明 |
---|---|
1 |
Add(String) 将字符串添加到末尾StringCollection。 |
2 |
AddRange(String[] ) 将字符串数组的元素复制到末尾 StringCollection。 |
3 | Clear() strong> 从 StringCollection 中删除所有字符串。 |
4 |
Contains(String) 判断指定字符串是否在 StringCollection。 |
5 |
copyTo(String[] ,Int32) 将整个 StringCollection 值复制到一维字符串数组,从指定位置开始 目标数组的索引。 |
6 |
Equals( Object) 判断指定对象是否等于 当前对象。 (继承自Object) |
7 |
GetEnumerator() 返回一个迭代的 StringEnumerator StringCollection。 |
现在让我们看一些示例
检查两个 StringCollection 对象是否相等,代码如下 -
示例
现场演示
using System; using System.Collections.Specialized; public class Demo { public static void Main() { StringCollection strCol1 = new StringCollection(); strCol1.Add("Accessories"); strCol1.Add("Books"); strCol1.Add("Electronics"); Console.WriteLine("StringCollection1 elements..."); foreach (string res in strCol1) { Console.WriteLine(res); } StringCollection strCol2 = new StringCollection(); strCol2.Add("Accessories"); strCol2.Add("Books"); strCol2.Add("Electronics"); Console.WriteLine("StringCollection2 elements..."); foreach (string res in strCol1) { Console.WriteLine(res); } Console.WriteLine("Both the String Collections are equal? = "+strCol1.Equals(strCol2)); } }
输出
这将产生以下输出 −
StringCollection1 elements... Accessories Books Electronics StringCollection2 elements... Accessories Books Electronics Both the String Collections are equal? = False
要检查指定的字符串是否在StringCollection中,代码如下 −
示例
在线演示
using System; using System.Collections.Specialized; public class Demo { public static void Main() { StringCollection stringCol = new StringCollection(); String[] arr = new String[] { "100", "200", "300", "400", "500" }; Console.WriteLine("Array elements..."); foreach (string res in arr) { Console.WriteLine(res); } stringCol.AddRange(arr); Console.WriteLine("Does the specified string is in the StringCollection? = "+stringCol.Contains("800")); } }
输出
这将产生以下输出 −
Array elements... 100 200 300 400 500 Does the specified string is in the StringCollection? = False
以上就是C# 中的 StringCollection 类的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。