简体   繁体   中英

List.contains on specific location

there are names inside the Titles array and i want to know what names are in there and i want to place the numbers on a separate array. Example i want to get all the names with this format: "Name 1","Name 2","Name 3" but what happens is that it gets also the names with format: "Name 1","Name1","SampleName1" and "FirstNameLast" because i am using a substring. I provided the code i have below.

List<int> ArrayCounter = new List<int>();

foreach(Title titlename in Titles)
{
    int length = "StringFromResource".length;
    if(titlename.name.length == length)
    {
        if (!UntitledPolicyArrayCounter.Contains(0))
            UntitledPolicyArrayCounter.Add(0);
    }
    else
    {
        if (!ArrayCounter.Contains(Convert.ToInt32(titlename.name.Substring(length + 1))))
            ArrayCounter.Add(Convert.ToInt32(titlename.name.Substring(length + 1)));
    }
}

i want to be able to get only the names with the format: "Name 1". How do i accomplish this without using regex?

You could use the StartsWith function. I'm still quite vague on what your code is trying to do. I feel example input and expected output might better clarify what you want. Here is some code that might help if I understand what you are trying to do. I might be a little off with my substring functions(you might need an additional +1).

http://msdn.microsoft.com/en-us/library/baketfxw.aspx

string findThis = "SomeTitle";
string name = "SomeTitle 1";

if(findThis.Length + 2 == name.Length
    && name.StartsWith(findThis))
{
  int number;
  //make sure there is exactly one space followed by an integer
  if( name.Substring(findThis.Length, 1) == " " 
    && Int32.TryParse(name.Substring(findThis.Length + 1, 1), out number))
  {
     //number now contains 1 parsed from the string
  } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM