本文整理汇总了C#中StringCollection.IndexOf方法的典型用法代码示例。如果您正苦于以下问题:C# StringCollection.IndexOf方法的具体用法?C# StringCollection.IndexOf怎么用?C# StringCollection.IndexOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringCollection
的用法示例。
在下文中一共展示了StringCollection.IndexOf方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InsertTest
public static void InsertTest(StringCollection collection, string[] data, string element, int location)
{
collection.Insert(location, element);
Assert.Equal(data.Length + 1, collection.Count);
if (element == ElementNotPresent)
{
Assert.Equal(location, collection.IndexOf(ElementNotPresent));
}
for (int i = 0; i < data.Length + 1; i++)
{
if (i < location)
{
Assert.Equal(data[i], collection[i]);
}
else if (i == location)
{
Assert.Equal(element, collection[i]);
}
else
{
Assert.Equal(data[i - 1], collection[i]);
}
}
}
示例2: IndexOfTest
public static void IndexOfTest(StringCollection collection, string[] data)
{
Assert.All(data, element => Assert.Equal(Array.IndexOf(data, element), collection.IndexOf(element)));
Assert.All(data, element => Assert.Equal(Array.IndexOf(data, element), ((IList)collection).IndexOf(element)));
Assert.Equal(-1, collection.IndexOf(ElementNotPresent));
Assert.Equal(-1, ((IList)collection).IndexOf(ElementNotPresent));
}
示例3: IndexOf_DuplicateTest
public static void IndexOf_DuplicateTest(StringCollection collection, string[] data)
{
// Only the index of the first element will be returned.
data = data.Distinct().ToArray();
Assert.All(data, element => Assert.Equal(Array.IndexOf(data, element), collection.IndexOf(element)));
Assert.All(data, element => Assert.Equal(Array.IndexOf(data, element), ((IList)collection).IndexOf(element)));
Assert.Equal(-1, collection.IndexOf(ElementNotPresent));
Assert.Equal(-1, ((IList)collection).IndexOf(ElementNotPresent));
}
示例4: runTest
public virtual bool runTest()
{
Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver: " + s_strDtTmVer);
int iCountErrors = 0;
int iCountTestcases = 0;
IntlStrings intl;
String strLoc = "Loc_000oo";
StringCollection sc;
string [] values =
{
"",
" ",
"a",
"aa",
"text",
" spaces",
"1",
"$%^#",
"2222222222222222222222222",
System.DateTime.Today.ToString(),
Int32.MaxValue.ToString()
};
try
{
intl = new IntlStrings();
Console.WriteLine("--- create collection ---");
strLoc = "Loc_001oo";
iCountTestcases++;
sc = new StringCollection();
Console.WriteLine("1. RemoveAt() from empty collection");
iCountTestcases++;
if (sc.Count > 0)
sc.Clear();
try
{
sc.RemoveAt(0);
iCountErrors++;
Console.WriteLine("Err_0001_{0}a, no exception");
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(" expected exception: " + ex.Message);
}
catch (Exception e)
{
iCountErrors++;
Console.WriteLine("Err_0001_{0}b, unexpected exception: " + e.ToString());
}
Console.WriteLine("2. RemoveAt() on filled collection");
strLoc = "Loc_002oo";
Console.WriteLine(" - at the beginning");
iCountTestcases++;
sc.Clear();
sc.AddRange(values);
if (sc.Count != values.Length)
{
iCountErrors++;
Console.WriteLine("Err_0002a, count is {0} instead of {1}", sc.Count, values.Length);
}
iCountTestcases++;
sc.RemoveAt(0);
if (sc.Count != values.Length - 1)
{
iCountErrors++;
Console.WriteLine("Err_0002b, Count returned {0} instead of {1}", sc.Count, values.Length - 1);
}
iCountTestcases++;
if (sc.Contains(values[0]))
{
iCountErrors++;
Console.WriteLine("Err_0002c, removed wrong item");
}
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
if (sc.IndexOf(values[i]) != i-1)
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}d, IndexOf returned {1} instead of {2}", i, sc.IndexOf(values[i]), i-1);
}
}
Console.WriteLine(" - at the end");
iCountTestcases++;
sc.Clear();
sc.AddRange(values);
if (sc.Count != values.Length)
{
iCountErrors++;
Console.WriteLine("Err_0002e, count is {0} instead of {1}", sc.Count, values.Length);
}
iCountTestcases++;
sc.RemoveAt(values.Length-1);
if (sc.Count != values.Length - 1)
{
iCountErrors++;
Console.WriteLine("Err_0002f, Count returned {0} instead of {1}", sc.Count, values.Length - 1);
}
iCountTestcases++;
if (sc.Contains(values[values.Length - 1]))
{
//.........这里部分代码省略.........
示例5: runTest
public virtual bool runTest()
{
Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver: " + s_strDtTmVer);
int iCountErrors = 0;
int iCountTestcases = 0;
IntlStrings intl;
String strLoc = "Loc_000oo";
StringCollection sc;
string [] values =
{
"",
" ",
"a",
"aa",
"text",
" spaces",
"1",
"$%^#",
"2222222222222222222222222",
System.DateTime.Today.ToString(),
Int32.MaxValue.ToString()
};
int cnt = 0;
try
{
intl = new IntlStrings();
Console.WriteLine("--- create collection ---");
strLoc = "Loc_001oo";
iCountTestcases++;
sc = new StringCollection();
Console.WriteLine("1. Check for empty collection");
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
if (sc.IndexOf(values[i]) != -1)
{
iCountErrors++;
Console.WriteLine("Err_0001_{0}, returned {1} for empty collection", i, sc.IndexOf(values[i]));
}
}
Console.WriteLine("2. add simple strings and verify IndexOf()");
strLoc = "Loc_002oo";
iCountTestcases++;
cnt = sc.Count;
sc.AddRange(values);
if (sc.Count != values.Length)
{
iCountErrors++;
Console.WriteLine("Err_0002a, count is {0} instead of {1}", sc.Count, values.Length);
}
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
if (sc.IndexOf(values[i]) != i)
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}b, IndexOf returned {1} instead of {0}", i, sc.IndexOf(values[i]));
}
}
Console.WriteLine("3. add intl strings and verify IndexOf()");
strLoc = "Loc_003oo";
string [] intlValues = new string [values.Length];
for (int i = 0; i < values.Length; i++)
{
string val = intl.GetString(MAX_LEN, true, true, true);
while (Array.IndexOf(intlValues, val) != -1 )
val = intl.GetString(MAX_LEN, true, true, true);
intlValues[i] = val;
}
int len = values.Length;
Boolean caseInsensitive = false;
for (int i = 0; i < len; i++)
{
if(intlValues[i].Length!=0 && intlValues[i].ToLower()==intlValues[i].ToUpper())
caseInsensitive = true;
}
iCountTestcases++;
cnt = sc.Count;
sc.AddRange(intlValues);
if ( sc.Count != (cnt + intlValues.Length) )
{
iCountErrors++;
Console.WriteLine("Err_0003a, count is {0} instead of {1}", sc.Count, cnt + intlValues.Length);
}
for (int i = 0; i < intlValues.Length; i++)
{
iCountTestcases++;
if (sc.IndexOf(intlValues[i]) != values.Length + i)
{
iCountErrors++;
Console.WriteLine("Err_0003_{0}b, IndexOf returned {1} instead of {2}", i, sc.IndexOf(intlValues[i]), values.Length + i);
}
}
Console.WriteLine("4. duplicate strings ");
strLoc = "Loc_004oo";
iCountTestcases++;
sc.Clear();
string intlStr = intlValues[0];
sc.Add(intlStr);
sc.AddRange(values);
//.........这里部分代码省略.........
示例6: runTest
public virtual bool runTest()
{
Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver: " + s_strDtTmVer);
int iCountErrors = 0;
int iCountTestcases = 0;
IntlStrings intl;
String strLoc = "Loc_000oo";
StringCollection sc;
string [] values =
{
"",
" ",
"a",
"aa",
"text",
" spaces",
"1",
"$%^#",
"2222222222222222222222222",
System.DateTime.Today.ToString(),
Int32.MaxValue.ToString()
};
int cnt = 0;
int ind = 0;
try
{
intl = new IntlStrings();
Console.WriteLine("--- create collection ---");
strLoc = "Loc_001oo";
iCountTestcases++;
sc = new StringCollection();
Console.WriteLine("1. add simple strings");
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
cnt = sc.Count;
sc.Add(values[i]);
if (sc.Count != cnt+1)
{
iCountErrors++;
Console.WriteLine("Err_0001_{0}a, count is {1} instead of {2}", i, sc.Count, cnt+1);
}
iCountTestcases++;
if (!sc.Contains(values[i]))
{
iCountErrors++;
Console.WriteLine("Err_0001_{0}b, collection doesn't contain new item", i);
}
iCountTestcases++;
ind = sc.IndexOf(values[i]);
if (ind != sc.Count - 1)
{
iCountErrors++;
Console.WriteLine("Err_0001_{0}c, returned index {1} instead of {2}", i, ind, sc.Count - 1);
}
if (ind != -1)
{
iCountTestcases++;
if (String.Compare(sc[ind], values[i], false) != 0)
{
iCountErrors++;
Console.WriteLine("Err_0001_{0}d, returned item \"{1}\" instead of \"{2}\"", i, sc[ind], values[i]);
}
}
}
Console.WriteLine("2. add intl strings");
string [] intlValues = new string [values.Length];
for (int i = 0; i < values.Length; i++)
{
string val = intl.GetString(MAX_LEN, true, true, true);
while (Array.IndexOf(intlValues, val) != -1 )
val = intl.GetString(MAX_LEN, true, true, true);
intlValues[i] = val;
}
Console.WriteLine(" initial number of items: " + sc.Count);
strLoc = "Loc_002oo";
for (int i = 0; i < intlValues.Length; i++)
{
iCountTestcases++;
cnt = sc.Count;
sc.Add(intlValues[i]);
if (sc.Count != cnt+1)
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}a, count is {1} instead of {2}", i, sc.Count, cnt+1);
}
iCountTestcases++;
if (!sc.Contains(intlValues[i]))
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}b, collection doesn't contain new item", i);
}
iCountTestcases++;
ind = sc.IndexOf(intlValues[i]);
if (ind != sc.Count - 1)
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}c, returned index {1} instead of {2}", i, ind, sc.Count - 1);
}
if (ind != -1)
//.........这里部分代码省略.........
示例7: valueExists
//检查某个Section下的某个键值是否存在
public bool valueExists(string Section, string Ident)
{
//
StringCollection Idents = new StringCollection();
readSection(Section, Idents);
return Idents.IndexOf(Ident) > -1;
}
示例8: runTest
//.........这里部分代码省略.........
}
catch (Exception e)
{
iCountErrors++;
Console.WriteLine("Err_0001f, unexpected exception: {0}", e.ToString());
}
Console.WriteLine("2. set Item on collection with simple strings");
strLoc = "Loc_002oo";
sc.Clear();
iCountTestcases++;
sc.AddRange(values);
int cnt = values.Length;
if (sc.Count != cnt)
{
iCountErrors++;
Console.WriteLine("Err_0002a, count is {0} instead of {1}", sc.Count, cnt);
}
for (int i = 0; i < cnt; i++)
{
iCountTestcases++;
sc[i] = values[cnt-i-1];
if (String.Compare(sc[i], values[cnt-i-1], false) != 0)
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}b, value is {1} instead of {2}", i, sc[i], values[cnt-i-1]);
}
}
Console.WriteLine("3. get Item on collection with intl strings");
strLoc = "Loc_003oo";
string [] intlValues = new string [values.Length];
for (int i = 0; i < values.Length; i++)
{
string val = intl.GetString(MAX_LEN, true, true, true);
while (Array.IndexOf(intlValues, val) != -1 )
val = intl.GetString(MAX_LEN, true, true, true);
intlValues[i] = val;
}
int len = values.Length;
Boolean caseInsensitive = false;
for (int i = 0; i < len; i++)
{
if(intlValues[i].Length!=0 && intlValues[i].ToLower()==intlValues[i].ToUpper())
caseInsensitive = true;
}
iCountTestcases++;
sc.Clear();
cnt = intlValues.Length;
sc.AddRange(intlValues);
if ( sc.Count != intlValues.Length )
{
iCountErrors++;
Console.WriteLine("Err_0003a, count is {0} instead of {1}", sc.Count, intlValues.Length);
}
for (int i = cnt; i < cnt; i++)
{
iCountTestcases++;
sc[i] = intlValues[cnt-i-1];
iCountTestcases++;
if (String.Compare(sc[i], intlValues[cnt-i-1], false) != 0)
{
iCountErrors++;
Console.WriteLine("Err_0003_{0}b, actual item is {1} instead of {2}", i, sc[i], intlValues[cnt-i-1]);
}
}
Console.WriteLine("4. case sensitivity");
strLoc = "Loc_004oo";
示例9: UpdateIds
private bool UpdateIds(StringCollection ids, DataTable table, UpdateMode mode)
{
bool updated = false;
if (mode == UpdateMode.New && ids.Count > 0)
{
ids.Clear();
updated = true;
}
foreach (DataRow row in table.Rows)
{
if (!row.IsNull(0))
{
string value = row[0].ToString();
if (mode == UpdateMode.Remove)
{
if (ids.IndexOf(value) >= 0)
{
ids.Remove(value);
updated = true;
}
}
else
{
if (mode == UpdateMode.Add)
{
if (ids.IndexOf(value) < 0)
{
ids.Add(value);
updated = true;
}
}
else
{
ids.Add(value);
updated = true;
}
}
}
}
return updated;
}
示例10: runTest
public virtual bool runTest()
{
Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver: " + s_strDtTmVer);
int iCountErrors = 0;
int iCountTestcases = 0;
IntlStrings intl;
String strLoc = "Loc_000oo";
StringCollection sc;
string [] values =
{
"",
" ",
"a",
"aa",
"text",
" spaces",
"1",
"$%^#",
"2222222222222222222222222",
System.DateTime.Today.ToString(),
Int32.MaxValue.ToString()
};
int cnt = 0;
try
{
intl = new IntlStrings();
Console.WriteLine("--- create collection ---");
strLoc = "Loc_001oo";
iCountTestcases++;
sc = new StringCollection();
Console.WriteLine("1. Remove() from empty collection");
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
sc.Remove(values[i]);
if (sc.Count != 0)
{
iCountErrors++;
Console.WriteLine("Err_0001_{0}, Remove changed Count for empty collection", i);
}
}
Console.WriteLine("2. add simple strings and test Remove()");
strLoc = "Loc_002oo";
iCountTestcases++;
sc.Clear();
sc.AddRange(values);
if (sc.Count != values.Length)
{
iCountErrors++;
Console.WriteLine("Err_0002a, count is {0} instead of {1}", sc.Count, values.Length);
}
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
if (!sc.Contains(values[i]))
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}b, doesn't contain {0} item", i);
}
cnt = sc.Count;
iCountTestcases++;
sc.Remove(values[i]);
if (sc.Count != cnt - 1)
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}c, didn't remove anything", i);
}
if (sc.Contains(values[i]))
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}d, removed wrong item", i);
}
}
Console.WriteLine("3. add intl strings and test Remove()");
strLoc = "Loc_003oo";
string [] intlValues = new string [values.Length];
for (int i = 0; i < values.Length; i++)
{
string val = intl.GetString(MAX_LEN, true, true, true);
while (Array.IndexOf(intlValues, val) != -1 )
val = intl.GetString(MAX_LEN, true, true, true);
intlValues[i] = val;
}
int len = values.Length;
Boolean caseInsensitive = false;
for (int i = 0; i < len; i++)
{
if(intlValues[i].Length!=0 && intlValues[i].ToLower()==intlValues[i].ToUpper())
caseInsensitive = true;
}
iCountTestcases++;
sc.Clear();
sc.AddRange(intlValues);
if ( sc.Count != intlValues.Length )
{
iCountErrors++;
Console.WriteLine("Err_0003a, count is {0} instead of {1}", sc.Count, intlValues.Length);
}
for (int i = 0; i < intlValues.Length; i++)
{
//.........这里部分代码省略.........