Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 19, 2014 String cast error | ||||
---|---|---|---|---|
| ||||
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 |
June 19, 2014 Re: String cast error | ||||
---|---|---|---|---|
| ||||
Posted in reply to SomeRiz | 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.
|
June 19, 2014 Re: String cast error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | 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 :( |
June 19, 2014 Re: String cast error | ||||
---|---|---|---|---|
| ||||
Posted in reply to SomeRiz | 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 |
June 19, 2014 Re: String cast error | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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) |
June 19, 2014 Re: String cast error | ||||
---|---|---|---|---|
| ||||
Posted in reply to SomeRiz | 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 |
June 19, 2014 Re: String cast error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | @Ali Çehreli Thanks I'm test code :) Successfully :) Thank you :) |
Copyright © 1999-2021 by the D Language Foundation