[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14900
  • Last Modified:

C# Trim a string, Get mid characters + right characters

I neeed to trim the following string to get a unique ID for a DataTable.

ProductTypeList:_ctl"3":ProductList:_ctl"10"

I have put quotes around the sections I need to extract. the numbers could be single or double digits but the first set will always have the first "_ctl" before and ":" after, the second set will always be at the end after the last "_ctl"

Thanks in advance

Glorfindal
0
glorfindal
Asked:
glorfindal
  • 2
  • 2
  • 2
  • +4
1 Solution
 
AxterCommented:
Hi glorfindal,
You should be able to use  LastIndexOf to get the last "_ctrl" position, and use the IndexOf method to get the first position of "_ctrl"

David Maisonave :-)
Cheers!
0
 
RealMrTeaCommented:
The following code will do what you want.  I did not include any error checking...you might want to make sure the string exists and all that but this should work fine.

                                                string startKey = "_ctl";
                  string endKey = ":";

                  //string input = "ProductTypeList:_ctl\"3\":ProductList:_ctl\"10\"";
                  string input = "ProductTypeList:_ctl3:ProductList:_ctl10";

                  int startKeyIndex = input.IndexOf(startKey);
                  int endKeyIndex = input.IndexOf(endKey, startKeyIndex);
                  int keyStartIndex = startKeyIndex + startKey.Length;
                  string strFirstValue = input.Substring(keyStartIndex, endKeyIndex - keyStartIndex);
                  Console.WriteLine(strFirstValue);

                  startKeyIndex = input.IndexOf(startKey, endKeyIndex);
                  endKeyIndex = input.Length;
                  keyStartIndex = startKeyIndex + startKey.Length;
                  string strSecondValue = input.Substring(keyStartIndex, endKeyIndex - keyStartIndex);
                  Console.WriteLine(strSecondValue);

Good Luck,
Eric
0
 
AxterCommented:
Example:


FirstNumberString = OriginalString.SubString(OriginalString.IndexOf("_ctrl")+5);
LastNumberString = OriginalString.SubString(OriginalString.LastIndexOf("_ctrl")+5);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
WinterMuteUKCommented:
Another way might be to split the string:

      string str = "ProductTypeList:_ctl3:ProductList:_ctl10:ProductList:_ctl2342:ProductList:_ctl7231:ProductList:_ctl99";
      string[] split = str.Split("l:".ToCharArray());
      for(int i = 2; i < split.Length; i = i+3)
            Console.WriteLine(split[i]);

Thanks

Wint.
0
 
WinterMuteUKCommented:
You could do the following if you want the values as integers:

      string str = "ProductTypeList:_ctl3:ProductList:_ctl10:ProductList:_ctl2342:ProductList:_ctl7231:ProductList:_ctl99";
      string[] split = str.Split("l:".ToCharArray());
      
      int[] numbers = new int[split.Length / 3]; //There is one number for each 3 'split' strings.
      
      int j = 0; //The counter to store values into the 'numbers' array.
      for(int i = 2; i < split.Length; i = i+3) //First value is found at index 2 (index[0] = 'ProductTypeList', index[1] = '_ctl')
      {
            numbers[j] = Convert.ToInt32(split[i]); //Insert to numbers array.
            j++; //Increment counter.
      }

You'd then have an array of the correct size filled with the numbers from the string.

Wint.
0
 
b1xml2Commented:
the best approach IMHO is to use regular expressions.
0
 
Bob LearnedCommented:
I agree with b1xml2:

    private string GenerateUniqueID(string input)
    {

      string pattern = @"[\d]+";

      Regex rgx = new Regex(pattern);

      string id = String.Empty;

      foreach (Match current in rgx.Matches(input))
      {
        id += current.Value;
      }

      return id;

    }

Usage:
string id = GenerateUniqueID(input);

Bob
0
 
AShumitaCommented:
The above suggestions do exactly what you asked for

but you can also use the method  <B>ControlName.NamingContainer.UniqueID</B> to get the unique Id of any component created.
0
 
Bob LearnedCommented:
Assuming ASP.NET, that is :)

Bob
0
 
glorfindalAuthor Commented:
Sorry it took so long getting back.

Thanks,
Glorfindal
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now