More elegant way then .Split(']');
-
Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :
string\[\] a = itm.Value.ToString().Split('\['); string\[\] b = a\[1\].ToString().Split('\]'); string first = a\[1\].ToString().Trim(); string second = b\[0\].ToString().Trim();
This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using
.Split()
Kind regards,
-
Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :
string\[\] a = itm.Value.ToString().Split('\['); string\[\] b = a\[1\].ToString().Split('\]'); string first = a\[1\].ToString().Trim(); string second = b\[0\].ToString().Trim();
This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using
.Split()
Kind regards,
You might be able to do something like (I'd normally use something like this for reading from a file, but it seems to work here too):
string ReadWord(string buffer, char delimiter, int startPos)
{
StringBuilder sb = new StringBuilder();
for(int i=startPos; i<buffer.Length; i++)
{
if(buffer[i] == delimiter)
break;sb.append(buffer\[i\]); } return sb.toString();
}
void main()
{
string firstPart = ReadWord(fullLine, ' ', 0);
int startPos = fullLine.IndexOf('[');
string secondPart = ReadWord(fullLine, ']', startPos);
}Thats just from memory so it may not compile, but you get the idea.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start -here[^]-
modified on Monday, May 18, 2009 7:42 AM
-
Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :
string\[\] a = itm.Value.ToString().Split('\['); string\[\] b = a\[1\].ToString().Split('\]'); string first = a\[1\].ToString().Trim(); string second = b\[0\].ToString().Trim();
This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using
.Split()
Kind regards,
Hi, I like using regex:
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"[ [\]]");
label1.Text = "";
foreach (String s in r.Split(textBox2.Text))
{
label1.Text += s + "\n";
}You might use index 0 and 2 and you don't need .Trim() again. ;-) Have fun
-
Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :
string\[\] a = itm.Value.ToString().Split('\['); string\[\] b = a\[1\].ToString().Split('\]'); string first = a\[1\].ToString().Trim(); string second = b\[0\].ToString().Trim();
This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using
.Split()
Kind regards,
-
Dear All, I have ListView full of things, but basically all the items have the same format : TY223 [16] TW121 [223] ... ... If I want to substract the value 16 and TY223 from TY223 [16] , I go about it like this :
string\[\] a = itm.Value.ToString().Split('\['); string\[\] b = a\[1\].ToString().Split('\]'); string first = a\[1\].ToString().Trim(); string second = b\[0\].ToString().Trim();
This works fine, however I'm only after the value 16. Is there a more elegant way of extracting this value from the item, instead of using
.Split()
Kind regards,
I think you mean "extract" the value. Regular Expression. I'd use something like:
"\[(?'Value'.*)\]"