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":Pr oductList: _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
ProductTypeList:_ctl"3":Pr
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
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\" :ProductLi st:_ctl\"1 0\"";
string input = "ProductTypeList:_ctl3:Pro ductList:_ ctl10";
int startKeyIndex = input.IndexOf(startKey);
int endKeyIndex = input.IndexOf(endKey, startKeyIndex);
int keyStartIndex = startKeyIndex + startKey.Length;
string strFirstValue = input.Substring(keyStartIn dex, endKeyIndex - keyStartIndex);
Console.WriteLine(strFirst Value);
startKeyIndex = input.IndexOf(startKey, endKeyIndex);
endKeyIndex = input.Length;
keyStartIndex = startKeyIndex + startKey.Length;
string strSecondValue = input.Substring(keyStartIn dex, endKeyIndex - keyStartIndex);
Console.WriteLine(strSecon dValue);
Good Luck,
Eric
string startKey = "_ctl";
string endKey = ":";
//string input = "ProductTypeList:_ctl\"3\"
string input = "ProductTypeList:_ctl3:Pro
int startKeyIndex = input.IndexOf(startKey);
int endKeyIndex = input.IndexOf(endKey, startKeyIndex);
int keyStartIndex = startKeyIndex + startKey.Length;
string strFirstValue = input.Substring(keyStartIn
Console.WriteLine(strFirst
startKeyIndex = input.IndexOf(startKey, endKeyIndex);
endKeyIndex = input.Length;
keyStartIndex = startKeyIndex + startKey.Length;
string strSecondValue = input.Substring(keyStartIn
Console.WriteLine(strSecon
Good Luck,
Eric
Example:
FirstNumberString = OriginalString.SubString(O riginalStr ing.IndexO f("_ctrl") +5);
LastNumberString = OriginalString.SubString(O riginalStr ing.LastIn dexOf("_ct rl")+5);
FirstNumberString = OriginalString.SubString(O
LastNumberString = OriginalString.SubString(O
Another way might be to split the string:
string str = "ProductTypeList:_ctl3:Pro ductList:_ ctl10:Prod uctList:_c tl2342:Pro ductList:_ ctl7231:Pr oductList: _ctl99";
string[] split = str.Split("l:".ToCharArray ());
for(int i = 2; i < split.Length; i = i+3)
Console.WriteLine(split[i] );
Thanks
Wint.
string str = "ProductTypeList:_ctl3:Pro
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:Pro ductList:_ ctl10:Prod uctList:_c tl2342:Pro ductList:_ ctl7231:Pr oductList: _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.
string str = "ProductTypeList:_ctl3:Pro
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]);
j++; //Increment counter.
}
You'd then have an array of the correct size filled with the numbers from the string.
Wint.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.NamingConta iner.Uniqu eID</B> to get the unique Id of any component created.
but you can also use the method <B>ControlName.NamingConta
Assuming ASP.NET, that is :)
Bob
Bob
ASKER
Sorry it took so long getting back.
Thanks,
Glorfindal
Thanks,
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!