Ruby Title Case
'some string here'.gsub(/\b\w/){$&.upcase}
12303 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
'some string here'.gsub(/\b\w/){$&.upcase}
_Bool strequals(char* a, char* b) { if (!a || !b) return 0; do {if (*a != *b) return 0; } while (*a++ && *b++); return 1; }
emails = emails.split(/,/).join().split()
dynamic class _String { // Replace a string or substrings within a string static function Replace (the_String, search_String, replace_String, occurrences, backward) { if (search_String == replace_String) return the_String; var found = 0; if (backward == true) { var pos = the_String.lastIndexOf(search_String); while (pos>= 0) { found++; var start_String = the_String.substr(0, pos); var end_String = the_String.substr(pos + search_String.length); the_String = start_String + replace_String + end_String; pos = the_String.lastIndexOf(search_String, start_String.length); if (found == occurrences) pos = -1; } } else { var pos = the_String.indexOf(search_String); while (pos>= 0) { found++; var start_String = the_String.substr(0, pos); var end_String = the_String.substr(pos + search_String.length); the_String = start_String + replace_String + end_String; pos = the_String.indexOf(search_String, pos + replace_String.length); if (found == occurrences) pos = -1; } } return the_String; } // Convert delimited (comma by default) String to an Array static function toArray(string, separator:String) { var list = new Array(); if (typeof(string) == "string"){ if (separator == undefined) separator = ","; if (string == null) return false; var currentStringPosition = 0; while (currentStringPosition<string.length) { var nextIndex = string.indexOf(separator, currentStringPosition); if (nextIndex == -1) break; var word = string.slice(currentStringPosition, nextIndex); list.push(word); currentStringPosition = nextIndex+1; } if (list.length<1) list.push(string); else list.push(string.slice(currentStringPosition, string.length)); } else { list.push(string); } return list; } }
#include <string> #include <iostream> #include <vector> #include <sstream> #include <iomanip> using namespace std; #ifndef ST_STRING #define ST_STRING enum CONVTYPE { STRTOINT, STRTOFLOAT, INTTOSTR, STRTODOUBLE }; /* * Convtype converts an integer to a string and a string * string to an integer, a float, or a double. The third argument * of this method determines what to do: * STRTOINT, STRTOFLOAT, INTTOSTR, and STRTODOUBLE. * */ void convtype(void*, void*, const int& ); #endif
void convtype(void* inp, void* out, const int& convtype) { istringstream isbuf; ostringstream osbuf; switch(convtype) { case STRTOINT: isbuf.str(*(string*) inp); isbuf >> *((int*) out); break; case STRTOFLOAT: isbuf.str(*(string*) inp); isbuf >> *((float*) out); break; case STRTODOUBLE: isbuf.str(*(string*) inp); isbuf >> *((double*) out); break; case INTTOSTR: osbuf << *((int*) inp); *((string*)out)=osbuf.str(); break; } return; }
"".split()
import re re.split('\W+', "This is an, example.")
re.findall('\w+', "This is an, example.")
import re mystring="This is an, example." re.findall(r'\w+', mystring) #all words re.findall(r'\w*i\w*', mystring) #all words containing letter i re.findall(r'\w{4,}', mystring) #all words >= 4 letters
"5445556747".using('(###) ###-####', '', true) => (544) 555-6747
require 'strscan' class String # Returns the string formatted according to a pattern. # # The pattern consists of placeholders and literals. The string is placed in # the placeholders, leaving the literals as they are. The result may be # truncated or padded if there are more placeholders than strings. # # Placeholders are '#' or '&'. Each '#' is replaced by one character from # the string, or the filler character if the string has no characters left. # The '&' is replaced by any remaining characters, or left out of the result # if there are no remaining characters. There can only be one '&' in the # pattern. If there is no '&', remaining characters are discarded. # # '#' or '&' may be replaced by other characters if they are needed as # literals. # # Examples: # "123456789".using('###-##-####') # => "123-45-6789" # "12345".using('###-##-####') # => "123-45" # "12345".using('###-##-####', nil) # => "12345" # "12345".using('###-##-####', ' ') # => "123-45- " # "5551212".using ('(###) ###-####', '', true) # => "555-1212" # "873555121276668".using ('(###) ###-#### ext &', '', true) # => "(873) 555-1212 ext 76668" # "KB5774X".using ('##-&-#') # => "KB-5774-X" # # Parameters: # pattern -- The format string, see above. # fill -- A string for padding. If the empty string, then the pattern is # filled as much as possible, and the rest of the pattern is # truncated. If nil, and the string does not fill the pattern, # the string is returned unchanged. Otherwise, the string is # padded to fill the pattern, which is not truncated. Defaults to # the empty string. # right -- If true, the pattern is filled from right-to-left instead of # from left-to-right, and truncated on the left instead of the # right if needed. Default is false. # fixchar -- The single-character placeholder. Default is '#'. # remchar -- The remaining-character placeholder. Default is '&'. # def using(pattern, fill='', right=false, fixchar='#', remchar='&') remCount = pattern.count(remchar) raise ArgumentError.new("Too many #{remchar}") if remCount > 1 raise ArgumentError.new("#{fixchar} too long") if fixchar.length > 1 raise ArgumentError.new("#{remchar} too long") if remchar.length > 1 raise ArgumentError.new("#{fill} too long") if fill.length > 1 remaining = remCount != 0 slots = pattern.count(fixchar) # Return the string if it doesn't fit and we shouldn't even try, if fill.nil? return self if self.length < slots return self if self.length > slots and !remaining end # Pad and clone the string if necessary. source = if fill.nil? || fill.empty? then self elsif right then self.rjust(slots, fill) else self.ljust(slots, fill) end # Truncate the string if necessary. if source.length > slots && !remaining then source = right ? source[-source.length, source.length] : source[0, source.length] end # Truncate pattern if needed. if !fill.nil? && fill.empty? then if source.length < slots # implies '&' can be ignored keepCount = source.length # Number of placeholders we are keeping leftmost, rightmost = 0, pattern.length - 1 if right then # Look right-to-left until we find the last '#' to keep. # Loop starts at 1 because 0th placeholder is in the inject param. leftmost = (1...keepCount).inject(pattern.rindex(fixchar)) { |leftmost, n| pattern.rindex(fixchar, leftmost - 1) } else # Look left-to-right until we find the last '#' to keep. rightmost = (1...keepCount).inject(pattern.index(fixchar)) { |rightmost, n| pattern.index(fixchar, rightmost + 1) } end pattern = pattern[leftmost..rightmost] slots = pattern.count(fixchar) end # Trim empty '&' up to nearest placeholder. If a '&' goes empty, the # literals between it and the nearest '#' are probably also unnecessary. if source.length == slots then if pattern.match("^#{Regexp.escape(remchar)}") then pattern = pattern[pattern.index(fixchar) || 0 ... pattern.length] elsif pattern.match("#{Regexp.escape(remchar)}$") then pattern = pattern[0 ... (pattern.rindex(fixchar) + fixchar.length) || pattern.length] end end end # Figure out how long the remainder will be when we get to it. remSize = source.length - slots if remSize < 0 then remSize = 0; end # Make the result. scanner = ::StringScanner.new(pattern) sourceIndex = 0 result = '' fixRegexp = Regexp.new(Regexp.escape(fixchar)) remRegexp = Regexp.new(Regexp.escape(remchar)) while not scanner.eos? if scanner.scan(fixRegexp) then result += source[sourceIndex].chr sourceIndex += 1 elsif scanner.scan(remRegexp) then result += source[sourceIndex, remSize] sourceIndex += remSize else result += scanner.getch end end result end end
<?php function stri_replace( $find, $replace, $string ) { // Case-insensitive str_replace() $parts = explode( strtolower($find), strtolower($string) ); $pos = 0; foreach( $parts as $key=>$part ){ $parts[ $key ] = substr($string, $pos, strlen($part)); $pos += strlen($part) + strlen($find); } return( join( $replace, $parts ) ); } function txt2html($txt) { // Transforms txt in html //Kills double spaces and spaces inside tags. while( !( strpos($txt,' ') === FALSE ) ) $txt = str_replace(' ',' ',$txt); $txt = str_replace(' >','>',$txt); $txt = str_replace('< ','<',$txt); //Transforms accents in html entities. $txt = htmlentities($txt); //We need some HTML entities back! $txt = str_replace('"','"',$txt); $txt = str_replace('<','<',$txt); $txt = str_replace('>','>',$txt); $txt = str_replace('&','&',$txt); //Ajdusts links - anything starting with HTTP opens in a new window $txt = stri_replace("<a href=\"http://","<a target=\"_blank\" href=\"http://",$txt); $txt = stri_replace("<a href=http://","<a target=\"_blank\" href=http://",$txt); //Basic formatting $eol = ( strpos($txt,"\r") === FALSE ) ? "\n" : "\r\n"; $html = '<p>'.str_replace("$eol$eol","</p><p>",$txt).'</p>'; $html = str_replace("$eol","<br />\n",$html); $html = str_replace("</p>","</p>\n\n",$html); $html = str_replace("<p></p>","<p> </p>",$html); //Wipes <br> after block tags (for when the user includes some html in the text). $wipebr = Array("table","tr","td","blockquote","ul","ol","li"); for($x = 0; $x < count($wipebr); $x++) { $tag = $wipebr[$x]; $html = stri_replace("<$tag><br />","<$tag>",$html); $html = stri_replace("</$tag><br />","</$tag>",$html); } return $html; } ?>
class String def titlecase() ignore_list = %w{of etc and by the for on is at to but nor or a via} capitalize_all_ex(ignore_list) end def titlecase!() ignore_list = %w{of etc and by the for on is at to but nor or a via} capitalize_all_ex!(ignore_list) end def capitalize_all(force_downcase = true) ignore_list = %w{} capitalize_all_ex(ignore_list, force_downcase) end def capitalize_all!(force_downcase = true) ignore_list = %w{} capitalize_all_ex!(ignore_list, force_downcase) end def capitalize_all_ex(ignore_list, force_downcase = true) # if force_downcase is true then the # string is, um, downcased first :-) if force_downcase self.downcase.gsub(/[\w\']+/){ |w| ignore_list.include?(w) ? w : w.capitalize } else self.gsub(/[\w\']+/){ |w| ignore_list.include?(w) ? w : w.capitalize } end end def capitalize_all_ex!(ignore_list, force_downcase = true) if force_downcase self.replace(self.downcase.gsub(/[\w\']+/){ |w| ignore_list.include?(w) ? w : w.capitalize }) else self.replace(self.gsub(/[\w\']+/){ |w| ignore_list.include?(w) ? w : w.capitalize }) end end end
<?php function trunc($phrase, $max_words) { $phrase_array = explode(' ',$phrase); if(count($phrase_array) > $max_words && $max_words > 0) $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...' return $phrase; } ?>