www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the correct function or cast to compare an unprintable ASCII

reply "Gary Miller" <aiguy roadrunner.com> writes:
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.
Mar 28 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Mar 28 2014
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Mar 28 2014