Sorting strings with numbers in Bash [duplicate]
Execute this sort -t _ -k 2 -g data.file -t separator -k key/column -g general numeric sort
Execute this sort -t _ -k 2 -g data.file -t separator -k key/column -g general numeric sort
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
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
^\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
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
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; }
You’ll want to use the isalpha() and isdigit() standard functions in <ctype.h>. char c=”a”; // or whatever if (isalpha(c)) { puts(“it’s a letter”); } else if (isdigit(c)) { puts(“it’s a digit”); } else { puts(“something else?”); }
Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn’t just turn “0” to a blank string). s.replaceFirst(“^0+(?!$)”, “”) The ^ anchor will make sure that the 0+ being matched is at the beginning of … Read more
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
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