String operation
-
Hi, I have following string. And I have to extract "931.3392ms" from the string. This line of string is not delimited by any character. How do I extract it? 8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA_LOAD_TYPE.Deep) 931.3392ms (1.7/11.4 KB)
-
-
Hi, I have following string. And I have to extract "931.3392ms" from the string. This line of string is not delimited by any character. How do I extract it? 8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA_LOAD_TYPE.Deep) 931.3392ms (1.7/11.4 KB)
Try This
string str = "8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA\_LOAD\_TYPE.Deep) 931.3392ms (1.7/11.4 KB)";
MessageBox.Show(
(((str.Substring(str.IndexOf(")") + 1)).
Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).
Substring(0, ((str.Substring(str.IndexOf(")") + 1)).
Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).IndexOf("(")))
);Rajesh B --> A Poor Workman Blames His Tools <--
-
Try This
string str = "8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA\_LOAD\_TYPE.Deep) 931.3392ms (1.7/11.4 KB)";
MessageBox.Show(
(((str.Substring(str.IndexOf(")") + 1)).
Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).
Substring(0, ((str.Substring(str.IndexOf(")") + 1)).
Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).IndexOf("(")))
);Rajesh B --> A Poor Workman Blames His Tools <--
-
This line of string is not delimited by spaces.Yes,this particular line always contains same number of elements. Is there any efficient method other than split?
SRKSHOME wrote:
This line of string is not delimited by spaces.
You already mentioned that, just checking since there were spaces in the example-string.
SRKSHOME wrote:
Yes,this particular line always contains same number of elements.
Split on some other character? Like the "("?
SRKSHOME wrote:
Yes,this particular line always contains same number of elements.
But the elements do not always have the same length?
SRKSHOME wrote:
Is there any efficient method other than split?
Yup, using regular expressions[^].
I are Troll :suss:
-
Try This
string str = "8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA\_LOAD\_TYPE.Deep) 931.3392ms (1.7/11.4 KB)";
MessageBox.Show(
(((str.Substring(str.IndexOf(")") + 1)).
Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).
Substring(0, ((str.Substring(str.IndexOf(")") + 1)).
Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).IndexOf("(")))
);Rajesh B --> A Poor Workman Blames His Tools <--
Gr8... very smart. :)
Thanks Md. Marufuzzaman
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
-
Try This
string str = "8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA\_LOAD\_TYPE.Deep) 931.3392ms (1.7/11.4 KB)";
MessageBox.Show(
(((str.Substring(str.IndexOf(")") + 1)).
Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).
Substring(0, ((str.Substring(str.IndexOf(")") + 1)).
Substring((str.Substring(str.IndexOf(")") + 1)).IndexOf(")") + 1)).IndexOf("(")))
);Rajesh B --> A Poor Workman Blames His Tools <--
I can't disagree more, that is horrible code. It is unreadable, it has 7 calls to IndexOf where you only need 3, and 7 to SubString where you also need just 3.
string a=str.Substring(str.IndexOf(")") + 1); // skip everything before first ")" string b=a.Substring(a.IndexOf(")") + 1); // skip everything before second ")" string c=b.Substring(0, b.IndexOf("(")); // drop everything after next "("
The Regex alternatives are pretty simple too, here is one; the result excludes the surrounding spaces:
string d=Regex.Match(str, @"\\) (\[0-9\\.\]\*ms) \\(").Groups\[1\].Value;
:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Hi, I have following string. And I have to extract "931.3392ms" from the string. This line of string is not delimited by any character. How do I extract it? 8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA_LOAD_TYPE.Deep) 931.3392ms (1.7/11.4 KB)
Use a Regular Expression, check out Expresso[^] or my own RegexTester[^].
-
Gr8... very smart. :)
Thanks Md. Marufuzzaman
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
+5 for sarcasm.
-
This line of string is not delimited by spaces.Yes,this particular line always contains same number of elements. Is there any efficient method other than split?
if the string is always fix size you can simply try the Substring method of the String class.
-
Hi, I have following string. And I have to extract "931.3392ms" from the string. This line of string is not delimited by any character. How do I extract it? 8 (26/04/2010 05:58.03) Execute(WQADataRequest, WQA_LOAD_TYPE.Deep) 931.3392ms (1.7/11.4 KB)
You may want to consider using my
StringParser
[^] class.string mSec = string.Empty;
StringParser sp = new StringParser (s);
if (sp.skipToEndOf (") ") && sp.skipToEndOf (") ")) {
sp.extractTo ("(", out mSec);
}/ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com