Unescape encoded string in Node (\x##) - node.js

I have the following encoded string in Node;
const test = '\x50\x77\x6b\x6d\x77\x37\x54\x43\x6f\x51\x3d\x3d'
I want to get it's unencoded value Pwkmw7TCoQ==
How can I achieve this?

Use the .toString() method. It should work.
test.toString()

Nothing to do there. Just print the string to the console as it is.
const test = '\x50\x77\x6b\x6d\x77\x37\x54\x43\x6f\x51\x3d\x3d';
console.log(test);
'\x50\x77\x6b\x6d\x77\x37\x54\x43\x6f\x51\x3d\x3d' and 'Pwkmw7TCoQ==' are different notations for the same value.

Related

How to generate a warning/error when using non-string-variables inside string interpolation?

TypeScript does not produce any errors for the following code:
const maybe_a_string: undefined | string = undefined;
const false_or_string: false | string = false;
// I'd like the following to produce an error/warning...
const message_string = `Some readable string info should be here: ${maybe_a_string} ${false_or_string}`;
Is there some kind of setting I can turn on, or simple alternative ways to write the last line that will warn me about trying to use non-string variables inside strings like this? (but without needing to add extra lines of code for every sub-string to be asserted individually)
I guess it treats them as fine because some types like bools, numbers and misc objects have a .toString() method...
But especially in the case of undefined (which actually doesn't have a .toString() method) - it's quite common for you to have a bug there, as the only time you really want to see the string "undefined" inside another string is for debugging purposes. But there's a lot of these bugs out there in the wild where end users are seeing stuff like "hello undefined" unintentionally.
Personally I would handle this by making the string template into a function. That way you can specify that the arguments must be strings.
const createMessageString = (first: string, second: string): string => {
return `Some readable string info should be here: ${first} ${second}`;
}
const message_string = createMessageString( maybe_a_string, false_or_string );
// will give an error unless types are refined
Vote for https://github.com/microsoft/TypeScript/issues/30239 [Restrict template literal interpolation expressions to strings]
Additionally, you can try workarounds from the issue comments.

I need to read a string while ignoring any spaces or capital letters

I'm trying to read any message sent on a discord server and send a reply if a certain string is within the message ignoring all spaces and capitals. I'm very new to javascript and this is the first code I'm making just for fun.
This is the current main part of the code.
if(msg.content.toLowerCase().includes('string'))
{
msg.channel.send(emoji("480351478930866179"));
}
You can remove whitespace with replace() and shift the string to lowercase using toLowerCase() to achieve the desired result.
const original = 'Hello there.';
const str = original.replace(/\s/g, '').toLowerCase();
if (str.includes('hello')) console.log('Hi.');
You could use the string.replace method or you could use split then join. To ignore case just use toLowerCase();
Thank's, that solved my problem.
const original = 'Hello there.';
const str = original.replace(/\s/g, '').toLowerCase();
if (str.includes('hello')) console.log('Hi.');

Typescript string manipulation not working

I have a ";" delimited string. I need to remove an entry from it. I tried to use slice but that does get sliced string but not the original-modified string.
Here is an example:
var str1: string = 'TY66447;BH31496;PA99001;';
var str2 = str1.slice(16, 23);
console.log(str1);
console.log(str2);
It gives:
TY66447;BH31496;PA99001;
PA99001
But what I want to achieve is TY66447;BH31496;
I am not sure if I am using the correct string method. Please guide how to achieve.
I don't understand what do you want, but it seems that you want:
str1.slice(0, 16);

print to string or convert AnyObject to string swift Azure

I invoke an API and after the answer call my method
func getLikes(result:AnyObject!, response:NSHTTPURLResponse! , error:NSError!){
println(result)
}
And when I try print "result" - I get normal JSON in console, but when I try to convert to String
var t = result as String
have "EXEC_BAD_ACCESS".
I try to do print to targetStream (like NSOutputStream), but I don't find how to do it.
How to convert json to string, any ideas?
Okay, i find solution:
If some tyype you can print, but don't know what it is - object implementation Printable interface(printable)
So, you can do that:
var t:String = result.description
And all, good day

How to implement string manipulation in efficienct way?

I have a string ="/show/search/All.aspx?Att=A1". How to get the last value after the 'Att=' in efficient way ?
You could do a split on the '=' character.
Example (in C#):
string line = "/show/search/All.aspx?Att=A1";
string[] parts = line.Split('=');
//parts[1] contains A1;
Hope this helps
If you're only dealing with this one URL then both of the other answers would work fine. I would consider using the HttpUtility.ParseQueryString method and just pull out the item you want by key.
Whatever an
efficient way
is...
Try this:
var str = "/show/search/All.aspx?Att=A1";
var searchString = "Att=";
var answer = str.Substring(str.IndexOf(searchString) + searchString.Length);

Resources