How to sort characters in a string e.g. "5121" -> "1125" ?
I can do this with code below but it seems too slow:
var nonSortedString = "5121"
var sortedString = String(Array(nonSortedString.characters).sort())
The CharacterView handles properly compound characters and provides proper ordering ("eěf" vs. "efě"). If you are ok with the way C++ handles unicode characters, try using one of the other views, like nonSortedString.utf16.sort(). It should give speed similar to C++.
Related
૮₍ • ᴥ • ₎ა・Raiden ▬▭⋱𓂅
ᘏ⑅ᘏ╭╯Welcome╰╮𓂃ᘏᗢ
・・・・・・・・・・・・・・・・・・・・・
https://discord.gg/rsCC8y7WC4
・・・・・・・・・・・・・・・・・・・・・
Join!
・・・・・・・・・・・・・・・・・・・・・
How can I pull the "discord.gg/rsCC8y7WC4" link from a text like this
console.log(invitelink) ==> discord.gg/rsCC8y7WC4
Use String.match() for this. String.match accepts a regex argument which looks like this:
let str = 'hey there this is just a random string';
let res = str.match(/random/);
//res is now ['random']
Now for your problem, you are probably looking for this:
if(msg.content.match(/discord\.gg\/.+/) || msg.content.match(/discordapp\.com\/invite\/.+/)) return msg.channel.send('Hey! You put an invite in your message!');
Now that regex may look a bit messy/complicated but the \s are to escape the character and make sure regex knows that it’s not the special character it uses, and is actually just a character part of the search. To clarify why the above example should work, here’s a little explanation:
match() returns either an array (if it gets a match) or null (if it gets no match). You are searching for the strings 'discord.gg/' followed by any characters and also checking for the string 'discordapp.com/invite/' also followed by any characters.
If this doesn’t work, please tell me.
I have an array made up of several strings that I am searching for in another array, like so:
strings_array = ["string1", "string2", "string3"]
main_array = [ ## this is populated with string values outside of my script ## ]
main_array.each { |item|
if strings_array.any? { |x| main_array.include?(x) }
main_array.delete(item)
end
}
This is a simplified version of what my script is actually doing, but that's the gist. It works as is, but I'm wondering how I can make it so that the strings_array can include strings made out of regex. So let's say I have a string in the main_array called "string-4385", and I want to delete any string that is composed of string- + a series of integers (without manually adding in the numerical suffix). I tried this:
strings_array = ["string1", "string2", "string3", /string-\d+/.to_s]
This doesn't work, but that's the logic I'm aiming for. Basically, is there a way to include a string with regex like this within an array? Or is there perhaps a better way than this .any? and include? combo that does the job (without needing to type out the complete string value)?
Thank you for any help!
You can use methods like keep_if and delete_if, so if you want to delete strings that match a regex you could do something like this:
array = ['string-123', 'test']
array.delete_if{|n| n[/string-\d+/] }
That will delete the strings in the array that do not match your regex. Same thing with keep_if method.
Hope it helps!
A good way to do this is with Regexp.union, which combines multiple regular expressions into a single regex handy for matching.
patterns = [/pattern1/, /pattern2/, /string-\d+/]
regex = Regexp.union(patterns)
main_array.delete_if{|string| string.match(regex)}
I'm looking for an idiomatic way to write a function List Char -> String in Purescript.
This seems like a simple thing to do, but I'm new to Purescript and have been browsing documentation for a while now with no progress!
Background information: I am porting a simple function from Haskell to Purescript
generateId :: Int -> [Char]
This generates a String of specified length. It was quite easy to convert the code to use List Char operations (where List is from Data.List in Purescript). In Haskell [Char] is the same as String so no other processing is needed, however, I can't find a function to convert from List Char to a native String in Purescript!
My search lead me to fromCharArray :: Array Char -> String in Data.String, however I could not find a way to convert from List Char to an Array Char!
I could manually convert between them by folding over List Char and building an Array Char using snoc, but surely I must be missing an inbuilt solution for what seems like basic String manipulation in Purescript!
Edit: fromList works to convert from any Unfoldable (such as Arrays) to a List. Still leaving this question open in case there is a more idiomatic way of achieving this.
I agree with your edit. Data.String.fromCharArray <<< Data.List.fromList sounds pretty decent to me. fromCharArray is implemented in native JS with array.join("").
Update: fromList is deprecated now - use toUnfoldable instead
[Updated Jan 2021]
In PureScript 0.13.8:
import Prelude
> import Data.Array (toUnfoldable)
> import Data.String.CodeUnits (fromCharArray)
> fromListChars = fromCharArray <<< toUnfoldable
> fromListChars ['2', '0', '2', '1']
"2021"
> fromListChars []
""
Silverlight is case sensitive for query string parameters so the following code would return false with "callid=5"
string callId;
if (System.Windows.Browser.HtmlPage.Document.QueryString.TryGetValue("callId", out callId))
{
....
}
Microsoft defends the decision by citing the www.w3.org spec, but I think it leads to a less friendly experience for people trying to link to you, or give a URL over the phone.
Looks like Stackoverflow is case insensitive:
https://stackoverflow.com/search?q=silverlight+bug
https://stackoverflow.com/search?Q=silverlight+bug
I think you should focus on your naming conventions rather than the implementations of standards, making sure to avoid similar field names and mixed case. For example, you can use a convention of words that over the phone can be read out stating "all lowercase" or "all uppercase".
I did this. Don't know if it helps.
var keyName = "";
if (!string.IsNullOrEmpty(keyName = someDictionary.SomeKeys.FirstOrDefault(k => k.ToLowerInvariant() == "size")))
{
var someValue = someDictionary[keyName];
}
Yes, I'm used to it being case sensitive, and therefore have been programming to it for a long time. I know of some people that have implemented methods to do intermediate parsing to convert them all to lowercase, or other things server side, and it really depends on what you are working with specifically.
As for usability, yes it is harder to read. BUT, at the same time a URL over the phone that has a querystring is not easy to give out anyway.
This workaround will not use the power of dictionaries because it will iterate through all keys, but it is likely to be a sufficient work-around for most scenarios.
var keyName = HtmlPage.Document.QueryString.Keys.SingleOrDefault(key => key.Equals("callid", StringComparison.OrdinalIgnoreCase));
string callid;
HtmlPage.Document.QueryString.TryGetValue(keyName, out callid)
You could also transform the whole QueryString dictionary to a new dictionary with a case insensitive comparer if you are having many dictionary lookups.
var insensitiveQueryString = HtmlPage.Document.QueryString.ToDictionary(pair => pair.Key, pair => pair.Value, StringComparer.OrdinalIgnoreCase);
I'm looking for one line code examples in various languages for getting a valid MD5 result (as a string, not a bytehash or what have you). For instance:
PHP:
$token = md5($var1 . $var2);
I found VB especially troublesome to do in one line.
C#:
string hash = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "md5");
VB is virtually the same.
Here it is not using the System.Web namespace:
string hash = Convert.ToBase64String(new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));
Or in readable form:
string hash =
Convert.ToBase64String
(new System.Security.Cryptography.MD5CryptoServiceProvider()
.ComputeHash
(System.Text.Encoding.UTF8.GetBytes
(input)
)
);
There is a kind of universality in how this is to be accomplished. Typically, one defines a routine called md5_in_one_line (or Md5InOneLine) once, and uses it all over the place, just as one would use a library routine.
So for example, once one defines Md5InOneLine in C#, it's an easy one-liner to get the right results.
Python
token = __import__('md5').new(var1 + var2).hexdigest()
or, if md5 is alrady imported:
token = md5.new(var1 + var2).hexdigest()
Thanks to Greg Hewgill
Aren't you really just asking "what languages have std. library support for MD5?" As Justice said, in any language that supports it, it'll just be a function call storing the result in a string variable. Even without built-in support, you could write that function in any language!
Just in case you need VBScript:
download the MD5 class from webdevbros and then with one line:
hash = (new MD5).hash("some value")
Does it really matter if you can do MD5 in one line. If it's that much trouble that you can't do it in VB in 1 line, then write your own function. Then, when you need to do MD5 in VB in one line, just call that function.
If doing it all in 1 line of code is all that important, here is 1 line of VB. that doesn't use the System.Web Namespace.
Dim MD5 As New System.Security.Cryptography.MD5CryptoServiceProvider() : Dim HashBytes() As Byte : Dim MD5Str As String = "" : HashBytes = MD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes("MyString")) : For i As Integer = 0 To HashBytes.Length - 1 : MD5Str &= HashBytes(i).ToString("x").PadLeft(2, "0") : Next
This will hash "MyString" and store the MD5 sum in MD5Str.
Coldfusion has a bunch of hashing algorithms, MD5 is the default.
cfset var md5hashresult = hash("string to hash")