Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-10 of 13 total  RSS 

Ruby Title Case

Capitalize all words in a string with the briefest of snippets!

'some string here'.gsub(/\b\w/){$&.upcase}

String equality tester

If you want to test the equality of two strings and don't want the overhead of strcmp(), then this is the function for you.
_Bool strequals(char* a, char* b) {
 if (!a || !b) return 0;
 do {if (*a != *b) return 0; } while (*a++ && *b++);
 return 1;
}

create an array or split a long string seperated by tabs newlines carriage returns and commas.

create an array or split a long string seperated by tabs newlines carriage returns and commas.

EG

emails = "skdjf@sdklfj.com sdflj@kj.com sldfj@klj.com, slkj@kjl.com,lskjdf@lkj.com, \r\nlkjkjl@kjk.com"

becomes

=> ["skdjf@sdklfj.com", "sdflj@kj.com", "sldfj@klj.com", "slkj@kjl.comlskjdf@lkj.com", "lkjkjl@kjk.com"]

emails = emails.split(/,/).join().split()

Actionscript _String Class

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;
	}
}

Convert String-Type the C++-Way

A very useful snipped that converts strings to/from various types.

Here's the header:
#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



The appropiate implementation:
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;
}

Python String Breaking and Beginners Reg Ex All-in-One !

http://mail.python.org/pipermail/python-list/2002-October/125367.html

// I found this on a python mailling list site

Ken wrote:
> "Padraig Brady" <Padraig@Linux.ie> wrote in message
> news:3D9AFA69.2020804@Linux.ie...
>
>>Ken wrote:
>>
>>>Hi all, I am trying to do a simple word search engine. Is there an easy
>>
> way
>
>>>to break up a sentence into individual words so that I can use it to
>>
> compare
>
>>>without traversing through every character?
>>>
>>>Eg, something like this:
>>>from: "This is an example"
>>>to: ["This", "is", "an", "example"]
>>
>>You can use

"".split()


but that will not
>>deal with punctuation. For that you will
>>need re.
>>
import re
re.split('\W+', "This is an, example.")

>>
>>This however will create an empty list item
>>for the last '.'
>>
>>You can get around this by using the converse:
re.findall('\w+', "This is an, example.")

>>
>>Pádraig.
>
Does string.rstrip() get rid of the commas, fullstops etc.?

Don't get mixed up between strip and split.
The help for rstrip says "returns a string with trailing whitespace removed".
I.E. "123 " -> "123" however "123 ." doesn't change.

> Also, can you explain what the parameters "re.findall('\w+', "This is an,
> example.")" mean?

The \w+ is a regular expression that matches one or more
characters in the set [a-zA-Z0-9_]. I.E. it matches words.
Anything else (like spaces, punctuation) is not returned.

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


Pádraig.

Format strings like PRINT USING, for Ruby

This is an extension to Ruby's String class. Just put this code in a file and include it. This basically takes a string and adds literals and padding to it. For example, you can format a phone number like this:

"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

Text to HTML converter (PHP 4+)

Simple function to convert a text into formatted HTML in PHP. The function implements some text cleanups (double space removal) and accepts some HTML in the text, like links (a href), lists (ul, ol), blockquotes and tables. This makes it perfect for use inside custom-made blogging engines and CMSs. There's also an implementation of case-insensitive search/replace for php < 5.

<?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('&quot;','"',$txt);
  $txt = str_replace('&lt;','<',$txt);
  $txt = str_replace('&gt;','>',$txt);
  $txt = str_replace('&amp;','&',$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>&nbsp;</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;
}

?>

Capitalizing titles in Ruby - another take...

This code was inspired by Peter's code from May 17...

I basically extended his idea to be more flexible and added ! methods to modify the string in place. I chose to use a different regex selector because using "\b" breaks on apostrophes and leave you with words like "You'Re" instead of "You're"...


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

Truncate String by Words

This will take a phrase and truncate it at the word level
<?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;
}
?>
« Newer Snippets
Older Snippets »
Showing 1-10 of 13 total  RSS