Thread overview
What is the correct function or cast to compare an unprintable ASCII character value to a string slice
Mar 28, 2014
Gary Miller
Mar 28, 2014
bearophile
Mar 28, 2014
monarch_dodra
March 28, 2014
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
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
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.