substring
C# String.Substring equivalent for StringBuilder?
The StringBuilder class has a special version of the ToString method that takes two arguments, exactly as Substring(startIndex, length). StringBuilder sb = new StringBuilder(“This is a Test”); string test = sb.ToString(10, 4); Console.WriteLine(test); // result = Test
How to extract numbers from a string?
You can use tr to delete all of the non-digit characters, like so: echo toto.titi.12.tata.2.abc.def | tr -d -c 0-9
Find one string in another with case insensitive in Objective-C
As similar to the answer provided in the link, but use options. See – (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask in Apple doc NSString *string = @”hello bla bla”; if ([string rangeOfString:@”BLA” options:NSCaseInsensitiveSearch].location == NSNotFound) { NSLog(@”string does not contain bla”); } else { NSLog(@”string contains bla!”); }
Find substring in string but only if whole words?
You can use regular expressions and the word boundary special character \b (highlight by me): Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore … Read more
Get index of substring
Use pointer subtraction. char *str = “sdfadabcGGGGGGGGG”; char *result = strstr(str, “abc”); int position = result – str; int substringLength = strlen(str) – position;
Mysql count instances of substring, then order by
SELECT (CHAR_LENGTH(str) – CHAR_LENGTH(REPLACE(str, substr, ”))) / CHAR_LENGTH(substr) AS cnt … ORDER BY cnt DESC Yep, looks bloated but afaik there is no any other possible solution. mysql> select (CHAR_LENGTH(‘asd’) – CHAR_LENGTH(REPLACE(‘asd’, ‘s’, ”))) / CHAR_LENGTH(‘s’); +—————————————————————–+ | (CHAR_LENGTH(‘asd’) – CHAR_LENGTH(REPLACE(‘asd’, ‘s’, ”))) / CHAR_LENGTH(‘s’) | +—————————————————————–+ | 1.0000 | +—————————————————————–+ 1 row in set … Read more
Limit length of longtext field in SELECT results
Use MySQL’s SUBSTRING function, as described in the documentation. Like: SELECT SUBSTRING(`text`, 1, 100) FROM blog_entry; To select first 100 chars.
MySQL Substring return empty value
The SUBSTR start index is 1 in mysql. So, you should change your code to: SELECT SUBSTR(FieldName,1,20) FROM TABLE; Notice that SUBSTR() is a synonym for SUBSTRING() so they can be used interchangeably. You should also checkout the documentation for SUBSTRING()