Extract using Split String


#1

20120421_PQR__24344341
20120421_PQR_AB_ 24344341
20120421_PQR_AB
24344341
20120421_PQR_AB
24344341
20120421_PQR_AB_MER__24344341
20120421_PQR_AB__24344341
20120421_PQR_AB___24344341
20120421_PQR_AB_PQRER_24344341
20120421_PQR_AB_PQRER__24344341
20120421_PQR_AB_PQRER__24344341
20120421_PQR_AB_WWF___24344341
20120421_PQR_WXYZ_BA_24344341
20120421_PQR_WXYZ__24344341
20120421_PQR_WXYZ___24344341
20120421_PQR_WXYZ_PQRER_24344341
PQR__20120421_224320
PQR_AB
20120421_224320
PQR_AB
20120421_224320
PQR_AB
_20120421_224320
PQR_AB_MER__20120421_224320
PQR_AB__20120421_224320
PQR_AB___20120421_224320
PQR_AB_PQRER_20120421_224320
PQR_AB_PQRER__20120421_224320
PQR_AB_PQRER__20120421_224320
PQR_AB_WWF___20120421_224320
PQR_WXYZ_BA_20120421_224320
PQR_WXYZ__20120421_224320
PQR_WXYZ___20120421_224320
PQR_WXYZ_PQRER_20120421_224320

In this string I want to extract only starting with PQR.

I used split string but couldn’t get to a result. I get only till first entry.


#2

If the entire thing is in a string split according to new line
use: StringVaraible.Split({vblf},StringSplitOptions.None)
for each item in array check for the condition if item.startswith(“PQR”)

Regards,
Vikas Reddy


#3

Guess vblf refers newline character as delimiter here , @vikas_reddy_Vicky can you please share keyword for tab space too.


#6

for space use: StringVaraible.Split({" "c},StringSplitOptions.None)

Regards,
Vikas Reddy


#7

This will be faster for you instead of looping through each line.

I looked up the syntax on filtering your array after the split and got something like this…
with text being the variable storing the long string
(From line In text.Split(vblf(0)) where line.StartsWith(“PQR”))(0).ToString

or to join the array back together…

[String].Join(vblf(0),(From line In text.Split(vblf(0)) where line.StartsWith(“PQR”))).ToString

Thanks.


#8

LINQ is based on IEnumerable, which executes by iterating over the collection and checking against the predicate. If the robot doesn’t add overhead, the difference should be compeltely negligible.
Note: I’m not saying to not use LINQ (I like it and use it often too), but for beginners using an explicit loop is usually easier than querying the collections.


#9

Good to know. Reason I made that suggestion (even if it’s harder to understand) is because when dealing with thousands of lines you kind of need a method that can drop the amount of lines down very quickly rather than having it loop for a long time.

As someone fairly new to LINQ myself, I find these solutions useful and more fun to use also. :stuck_out_tongue:


#10

hi,

Please follow the below tutorial,

Thank,
akrthik