Opposite of putchar()? - putchar

Is there an opposite to putchar() where you can pass the ascii character and it will output the numerical value? Thanks.

printf("%d\n", c);

I am not sure which language you are talking. In 'C++', you could just pass (int) ch which should return the numeraical ascii value.

Related

Fastest way to compare AnsiString

What would be the fastest way to check if an AnsiString equals some other AnsiString?
Currently i am doing this to check if the string is equal:
if AnsiCompareStr(mystring, 'helloworld') = 0 then
ShowMessage('equal');
Also what would be the fastest way to check if a AnsiString contains another AnsiString (not complete Check)?
For this i am using:
StrPos(mystring, 'world') <> nil then
ShowMessage('contains');
The answer is: it depends.
It depends on the type of strings, how they look, how long they are, and on which platform you are.
For any string comparison function that anyone comes up with here, I can come up with a scenario where that function is not the fastest solution.
AFAIK CompareText() is faster than AnsiCompareStr(), but only handle ASCII characters.
If you want a faster test of contained text, check https://stackoverflow.com/a/1554544/458259
Fastest case sensitive way is standard compare:
if Str1 = Str2 then (case sensitive)
It's faster than CompareStr()
Or CompareText - case insensitive

Delphi search for text after a defined character

I have a string
String :='this is my string | yes';
I need delphi to get whatever the text is after the |
So something like:
getTextAftertoCharacter('|',String);
This should return "yes" in the example above.
Has Delphi get a function for this?
I'm not aware of a single function that does this. I think the easiest way would be to use Pos and then Copy:
answer := Copy(str, Pos('|', str) + 1, Length(str));
Start character is at Pos('|', str) + 1, amount of characters to copy is actually Length(str)-Pos('|', str), but passing a greater value to Copy also works.
Note: this will return the whole contents of str if there is no '|'. Check for Pos('|', str) being non-zero if you need different behavior.
You can use Controls.GetLongHint():
GetLongHint(String);
That's because the separator of a two part hint is a |. In your example it would return ' yes', with the leading space. If | is not found, the function returns the String.
I'd use then following code:
Trim(RightStr(Text, Length(Text)-LastDelimiter('|', Text)));
It'll locate the last delimiter in the string and I used Trim to get rid of the whitespace. LastDelimiter can take more than one delimiter which may be usefull. If no delimiter is found it will return the whole string.

print carriage return (not line feed) in erlang?

Wondering if it's possible to print a carriage return without a line feed in erlang? i.e. equivalent to printf("this will be replaced next time \r"); in C.
Had a look through the io:format() documentation and didn't see anything. It only seems to support ~n, equivalent to carriage return+line feed pair ('\n' in C).
Thx.
"\r" is a perfectly valid escape sequence in Erlang. So you can do just
io:format("\r").
Check the reference manual for other escape sequences.
You can use \r in a string for the return character so:
io:format("Counter value: ~b\r", [Counter])
This is also works for character constants, $\r, and in quoted atoms.
Doh. Answer came almost as soon as I posted. ~c enables printing of ASCII characters, so it's just a case of printing ASCII carriage return (13). e.g.
io:format("Counter value: ~b~c", [Counter,13])
Still interested in anything more elegant...
Thx.

How to make sure a number is float in Erlang?

io:format throws a badarg exception if the format is ~f but the argument is integer:
io:format("~f", [2]).
Adding 0.0 solves the problem bus there an elegant way?
io:format("~f", [2+0.0]).
float(Number) convert Number to float
list_to_float(String) convert String to float
is_float(Term) returns true if Term is a float
If you don't care about the exact output, you can use:
io:format("~p", [Term]).
This will work with any term, but doesn't give you the same kind of formatting options as ~f would.
Either
io:format("~f", [2.0]).
or
io:format("~f", [float(2)]).
works.

Error in string.gsub with backslash

local a = "te\st"
local b = string.gsub(a,'\','\\\\')
assert(false,b)
What am I doing wrong?
When I do assert, I want that to the screen the string te\st will be printed... but it's not working
I have a JSON file, that I want to decode it into Lua table. I don't need to print out nothing, I did the assert just to test a local problem.
So what I need is to keep all data in the JSON file that has '\'.
Use [[]] instead of "" or '' if you don't want backslash to have special meaning.
Read about literal strings in the manual.
Have you tried escaping it with the % character instead of \
I don't know if this will help, but I was having a HELL of a time making Lua's gsub match my string with special characters in it that I wanted treated literally... it turned out that instead of using \ as an escape character, or doubling the character, that I needed to prefix the special character with % to make it be treated literally.
Your question wasn't too clear so I'm not 100% sure what you mean. Do you mean that you want the assert to fire when b is equal to the string "te\st"? If so you can do a simple:
assert(b ~= "te\st")
Or I suppse...
assert(b ~= a)
You don't need the gsub. But here it is anyways.
local a = "te\\st"
local b = string.gsub(a,'\\','\\')
assert(false,b)

Resources