Link to home
Start Free TrialLog in
Avatar of glorfindal
glorfindal

asked on

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
Avatar of Axter
Axter
Flag of United States of America image

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!
Avatar of RealMrTea
RealMrTea

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
Example:


FirstNumberString = OriginalString.SubString(OriginalString.IndexOf("_ctrl")+5);
LastNumberString = OriginalString.SubString(OriginalString.LastIndexOf("_ctrl")+5);
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Assuming ASP.NET, that is :)

Bob
Avatar of glorfindal

ASKER

Sorry it took so long getting back.

Thanks,
Glorfindal