Thread overview | |||||
---|---|---|---|---|---|
|
March 28, 2014 What is the correct function or cast to compare an unprintable ASCII character value to a string slice | ||||
---|---|---|---|---|
| ||||
If I need to examine a byte in string for a specific ASCII value like string MyString; MyString = "Hello World" and I want to check a position in the string for a certain unprintable ASCII value like the ASCII Block 219 what is the recommended method for doing so if (MyString[0..1])== ???(219)) writeln("ASCII Block found") I know there's and easy lib function or cast somewhere but everywhere I've looked it's not jumping out at me. |
March 28, 2014 Re: What is the correct function or cast to compare an unprintable ASCII character value to a string slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Miller | Gary Miller:
> if (MyString[0..1])== ???(219)) writeln("ASCII Block found")
enum char block = 219;
...
if (myString[i] == block)
"ASCII Block found".writeln;
Note that variable/function names in D start with a lower case.
Bye,
bearophile
|
March 28, 2014 Re: What is the correct function or cast to compare an unprintable ASCII character value to a string slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Miller | On Friday, 28 March 2014 at 15:33:32 UTC, Gary Miller wrote: > If I need to examine a byte in string for a specific ASCII value like > > string MyString; > > MyString = "Hello World" > > and I want to check a position in the string for a certain unprintable ASCII value like the ASCII Block 219 what is the recommended method for doing so > > if (MyString[0..1])== ???(219)) writeln("ASCII Block found") > > I know there's and easy lib function or cast somewhere but everywhere I've looked it's not jumping out at me. 219 is not an "ASCII Block". You can just do a search for 219 in your *byte* stream: auto b = ("hello" ~ cast(char)219 ~ "world").representation.canFind(219); The "representation" part *very* is important: Without it, you string will be interpreted as unicode, and there is *not* actually a 219 codepoint in there. |
Copyright © 1999-2021 by the D Language Foundation