• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59
  • Last Modified:

How to extract value from Data pattern

Hello Experts,

How can I extract numbers from the following strings:

"P:200:4401:25:000565 | Sanar Co | 1045 | IMR"   -- Extract 1045
"P/200/6401/35:000665 | Sanar Co | 1046 | IMR" -- Extract 1046
"P-500-6401-45-555665 | Sanar Co | 1049 | IMR" -- Extract 1049

Thanks.
0
JimiJ13
Asked:
JimiJ13
  • 3
  • 2
1 Solution
 
d_yorkCommented:
if the data is well formatted, use String.Split().  You could stick it in a foreach loop, but here I'll split one of your strings so you can see:
            string _test = "P:200:4401:25:000565 | Sanar Co | 1045 | IMR";
            string[] _split = _test.Split('|');
            int _number = Convert.ToInt32(_split[2]);
0
 
JimiJ13I T ConsultantAuthor Commented:
d_york,

Thanks. Actually, that's already my existing solution but, that doesn't work with "/", "-", & ":" present in the string.


Any idea?
0
 
d_yorkCommented:
works in English in C# .Net for all those strings - those are not metacharacters.  I hate to say "normal" but this should work on a "normal" setup without issue.  Something else is going on, you've got more going on than a simple set of pipe delimited lines of text.  You'll probably have to divulge more information to get assistance from those more worldly than me.  But if I were just handed this issue to debug in code I'd be looking at:
1) Encoding:  If I'm reading delimited text from file am I really getting what I think I'm getting?  I'd be looking at the language and the encoding being detected.
2)  Operators:  If I've got particular issues around the characters you mention I'd be thinking they are being treated as operators somewhere in the code.  And what I'm getting isn't a "string" but an object like a DateTime or Double or something.
3)  Dev Environment:  I'd suspect the coder was maybe coding toward Mono on Linux or something and it's a compiler or other issue.

Based just on what you posted, I had zero issue splitting those lines of text and converting the third element to an Int.  But I'm "seeing" plain text in English - I'm not seeing all the other factors that could be in play here.
0
 
JimiJ13I T ConsultantAuthor Commented:
d_york,

You are correct. I traced the issue on the Dot Net coding that concatenated the string thereby resulted to erroneous data extraction.  
 

Thanks.
0
 
JimiJ13I T ConsultantAuthor Commented:
Thanks for the quick response.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now