converting a number base 10 to base 62 (a-zA-Z0-9)

OLD: A quick and dirty solution can be to use a function like this: function toChars($number) { $res = base_convert($number, 10,26); $res = strtr($res,’0123456789′,’qrstuvxwyz’); return $res; } The base convert translate your number to a base where the digits are 0-9a-p then you get rid of the remaining digits with a quick char substitution. As … Read more

How to strip all non alphanumeric characters from a string in c++?

Write a function that takes a char and returns true if you want to remove that character or false if you want to keep it: bool my_predicate(char c); Then use the std::remove_if algorithm to remove the unwanted characters from the string: std::string s = “my data”; s.erase(std::remove_if(s.begin(), s.end(), my_predicate), s.end()); Depending on your requirements, you … Read more

Regex for alphanumeric, but at least one letter

^\d*[a-zA-Z][a-zA-Z0-9]*$ Basically this means: Zero or more ASCII digits; One alphabetic ASCII character; Zero or more alphanumeric ASCII characters. Try a few tests and you’ll see this’ll pass any alphanumeric ASCII string where at least one non-numeric ASCII character is required. The key to this is the \d* at the front. Without it the regex … Read more

How to determine if a String has non-alphanumeric characters?

Using Apache Commons Lang: !StringUtils.isAlphanumeric(String) Alternativly iterate over String’s characters and check with: !Character.isLetterOrDigit(char) You’ve still one problem left: Your example string “abcdefà” is alphanumeric, since à is a letter. But I think you want it to be considered non-alphanumeric, right?! So you may want to use regular expression instead: String s = “abcdefà”; Pattern … Read more

Generate a random alphanumeric string in Cocoa

Here’s a quick and dirty implementation. Hasn’t been tested. NSString *letters = @”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″; -(NSString *) randomStringWithLength: (int) len { NSMutableString *randomString = [NSMutableString stringWithCapacity: len]; for (int i=0; i<len; i++) { [randomString appendFormat: @”%C”, [letters characterAtIndex: arc4random_uniform([letters length])]]; } return randomString; }

How to remove all non-alpha numeric characters from a string in MySQL?

Using MySQL 8.0 or higher Courtesy of michal.jakubeczy’s answer below, replacing by Regex is now supported by MySQL: UPDATE {table} SET {column} = REGEXP_REPLACE({column}, ‘[^0-9a-zA-Z ]’, ”) Using MySQL 5.7 or lower Regex isn’t supported here. I had to create my own function called alphanum which stripped the chars for me: DROP FUNCTION IF EXISTS … Read more

How do I sort strings alphabetically while accounting for value when a string is numeric?

Pass a custom comparer into OrderBy. Enumerable.OrderBy will let you specify any comparer you like. This is one way to do that: void Main() { string[] things = new string[] { “paul”, “bob”, “lauren”, “007”, “90”, “101”}; foreach (var thing in things.OrderBy(x => x, new SemiNumericComparer())) { Console.WriteLine(thing); } } public class SemiNumericComparer: IComparer<string> { … Read more