www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - String cast error

reply "SomeRiz" <goren.ali yandex.com> writes:
Hi.

My code running:

http://dpaste.dzfl.pl/2183586524df

Output:

SerialNumber
927160020XXXX (X = Some Numbers)

How do I delete "SerialNumber" text?

Example

string SomeRiz = system(a);

I get an error:

b.d(10): Error: cannot implicitly convert expression (system(a)) 
of type int to
string

Later;

string SomeRiz = cast(string)system(a);

I get an error 2:

b.d(10): Error: cannot cast system(a) of type int to string


How do I delete "SerialNumber" text?

I just, want to see the numbers:

927160020XXXX

Sorry for my bad english
Jun 18 2014
parent reply Justin Whear <justin economicmodeling.com> writes:
On Thu, 19 Jun 2014 00:05:36 +0000, SomeRiz wrote:

 Hi.
 
 My code running:
 
 http://dpaste.dzfl.pl/2183586524df
 
 Output:
 
 SerialNumber 927160020XXXX (X = Some Numbers)
 
 How do I delete "SerialNumber" text?
 
 Example
 
 string SomeRiz = system(a);
 
 I get an error:
 
 b.d(10): Error: cannot implicitly convert expression (system(a))
 of type int to string
 
 Later;
 
 string SomeRiz = cast(string)system(a);
 
 I get an error 2:
 
 b.d(10): Error: cannot cast system(a) of type int to string
 
 
 How do I delete "SerialNumber" text?
 
 I just, want to see the numbers:
 
 927160020XXXX
 
 Sorry for my bad english
The problem is that `system` returns the process exit code (an integer), not the output of the process. Try using std.process.execute or std.process.executeShell.
Jun 18 2014
parent reply "SomeRiz" <goren.ali yandex.com> writes:
Hi Justin thank you.

I'm using

executeShell(a);

Output:

ProcessOutput(0, "SerialNumber    \r\r\n92716002xxxx 
\r\r\n\r\r\n")

How do I delete ProcessOutPut, 0, SerialNumber, \r,\n text?

I want to see just out: 92716002xxxx

Sorry for my bad english :(
Jun 18 2014
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Jun 19, 2014 at 12:31:51AM +0000, SomeRiz via Digitalmars-d-learn wrote:
 Hi Justin thank you.
 
 I'm using
 
 executeShell(a);
 
 Output:
 
 ProcessOutput(0, "SerialNumber    \r\r\n92716002xxxx \r\r\n\r\r\n")
 
 How do I delete ProcessOutPut, 0, SerialNumber, \r,\n text?
 
 I want to see just out: 92716002xxxx
 
 Sorry for my bad english :(
Try this: import std.regex; string extractSerial(string input) { auto m = input.match(`SerialNumber\s+(\S+)\s+`); if (m) return m.captures[1]; else throw new Exception("Could not find serial number"); } auto input = "SerialNumber \r\r\n92716002xxxx \r\r\n\r\r\n"; writeln(extractSerial(input)); // prints "92716002xxxx" Hope this helps. T -- You have to expect the unexpected. -- RL
Jun 18 2014
parent reply "SomeRiz" <goren.ali yandex.com> writes:
Thanks Teoh

I'm trying compile but i get an error:

b.d(22): Error: function b.extractSerial (string input) is not 
callable using ar
gument types (ProcessOutput)
Jun 18 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/18/2014 06:04 PM, SomeRiz wrote:
 Thanks Teoh

 I'm trying compile but i get an error:

 b.d(22): Error: function b.extractSerial (string input) is not callable
 using ar
 gument types (ProcessOutput)
According to its documentation, executeShell() returns a Tuple consisting of the return status and the output of the executed program: http://dlang.org/library/std/process/executeShell.html Do not print the entire returned Tuple. Instead, print just the .status member of it: auto result = executeShell(a); if (result.status == 0) { // It worked! Now we can get the output: auto output = result.output; // Use 'output' here... } So, 'output' above is what you should pass to extractSerial() function that H. S. Teoh has written: assert(extractSerial(output) == "92716002xxxx"); (I have not tested the code above.) Ali
Jun 18 2014
parent "SomeRiz" <goren.ali yandex.com> writes:
 Ali Çehreli

Thanks I'm test code :) Successfully :)

Thank you :)
Jun 18 2014