Top-rated recipes tagged "string"http://code.activestate.com/recipes/tags/string/top/2015-01-24T21:21:27-08:00ActiveState Code RecipesScramble a string of words, preserving spaces (Python) 2010-06-25T20:44:04-07:00Kai Malleahttp://code.activestate.com/recipes/users/4174276/http://code.activestate.com/recipes/577275-scramble-a-string-of-words-preserving-spaces/ <p style="color: grey"> Python recipe 577275 by <a href="/recipes/users/4174276/">Kai Mallea</a> (<a href="/recipes/tags/scramble/">scramble</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/words/">words</a>). </p> <p>The following function will scramble a string, preserving spaces that separate the words, and return the result. I used this for a word game that scrambled words and phrases, which a player then attempted to descramble. For example, scrambling the following string: "teenage mutant ninja turtles" would return "etegnae tamtnu jnnai urtltes"</p> Reversing string (JavaScript) 2013-02-18T09:27:33-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578464-reversing-string/ <p style="color: grey"> JavaScript recipe 578464 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/reverse/">reverse</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>Two ways reverse string.</p> converting numbers to their alphabetical style (Python) 2011-03-17T06:57:06-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577607-converting-numbers-to-their-alphabetical-style/ <p style="color: grey"> Python recipe 577607 by <a href="/recipes/users/4177294/">amir naghavi</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/string/">string</a>). Revision 4. </p> <p>converts numbers to alphabetical numbers </p> Approximately Equal (Python) 2010-03-17T17:05:51-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577124-approximately-equal/ <p style="color: grey"> Python recipe 577124 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/comparisons/">comparisons</a>, <a href="/recipes/tags/distance/">distance</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/numeric/">numeric</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>Generic "approximately equal" function for any object type, with customisable error tolerance.</p> <p>When called with float arguments, approx_equal(x, y[, tol[, rel]) compares x and y numerically, and returns True if y is within either absolute error tol or relative error rel of x, otherwise return False. The function defaults to sensible default values for tol and rel.</p> <p>For any other pair of objects, approx_equal() looks for a method __approx_equal__ and, if found, calls it with arbitrary optional arguments. This allows types to define their own concept of "close enough".</p> Use PrettyTable and xtopdf to create PDF tables with borders, alignment and padding (Python) 2015-01-24T21:21:27-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579012-use-prettytable-and-xtopdf-to-create-pdf-tables-wi/ <p style="color: grey"> Python recipe 579012 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/prettytable/">prettytable</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/tabular/">tabular</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to create tabular data in PDF format, supporting neat borders, and alignment and padding of columns, using the Python libraries called PrettyTable and xtopdf.</p> Random Password Generation (Python) 2014-08-10T15:50:40-07:00Paul Wolfhttp://code.activestate.com/recipes/users/4190553/http://code.activestate.com/recipes/578920-random-password-generation/ <p style="color: grey"> Python recipe 578920 by <a href="/recipes/users/4190553/">Paul Wolf</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/strong/">strong</a>). </p> <p>Generate a password (or other secure token) using a pattern language similar to regular expressions. We'll use the <code>strgen</code> module that enables a user to generate test data, unique ids, passwords, vouchers or other randomized data very quickly using a template language. The template language is superficially similar to regular expressions but instead of defining how to match or capture strings, it defines how to generate randomized strings.</p> String length (Batch) 2013-10-10T16:27:48-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578664-string-length/ <p style="color: grey"> Batch recipe 578664 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/length/">length</a>, <a href="/recipes/tags/string/">string</a>). Revision 2. </p> <p>A way for checking string length.</p> Split string sample (Batch) 2013-08-13T11:29:44-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578638-split-string-sample/ <p style="color: grey"> Batch recipe 578638 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>Target: we have a task to split the string ("this is a string") on the characters. Possible solution:</p> Formating strings (print a table) (Batch) 2013-06-18T07:52:03-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578562-formating-strings-print-a-table/ <p style="color: grey"> Batch recipe 578562 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/table/">table</a>). </p> <p>As you know there are no escape characters such as "\t" in the windows command language but it does not mean that we can not format text. Command prompt has its own tricks. At firstly, you need declare enabledelayedexpansion after setlocal command in your batch file to get access for some interesting things; secondly, use &lt;code&gt;&lt;nul set /p "str=[string]"&lt;/code&gt; construction which is equal print function in C language. OK, next batch file print multiplication table.</p> String to Binary (Python) 2014-07-30T07:20:22-07:00p@ntut$http://code.activestate.com/recipes/users/4183895/http://code.activestate.com/recipes/578291-string-to-binary/ <p style="color: grey"> Python recipe 578291 by <a href="/recipes/users/4183895/">p@ntut$</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/to/">to</a>). Revision 2. </p> <p>String to binary snippet, python3+</p> Bytes-to-human / human-to-bytes converter (Python) 2012-02-02T16:09:52-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578019-bytes-to-human-human-to-bytes-converter/ <p style="color: grey"> Python recipe 578019 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/humanize/">humanize</a>, <a href="/recipes/tags/string/">string</a>). Revision 15. </p> <p>Here goes.</p> Easy ansi color plus more. (Ruby) 2014-04-16T15:36:04-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578018-easy-ansi-color-plus-more/ <p style="color: grey"> Ruby recipe 578018 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/ansi/">ansi</a>, <a href="/recipes/tags/clear/">clear</a>, <a href="/recipes/tags/console/">console</a>, <a href="/recipes/tags/cursor/">cursor</a>, <a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/movement/">movement</a>, <a href="/recipes/tags/output/">output</a>, <a href="/recipes/tags/ruby/">ruby</a>, <a href="/recipes/tags/screen/">screen</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/underline/">underline</a>). </p> <p>This quick class extends the base String class to add the ability to chain escape codes onto your output. For instance: puts 'String'.bold.underline.blink.red for something truly hideous. Aside from the colors (all are supported, but I haven't put in support for background colors as of the time of this post), cursor placement (ymmv based on the term type), screen clearing, bold, underline, blink reverse, conceal are all handled as well.</p> Implementation of Knuth–Morris–Pratt algorithm (C) 2011-10-15T00:33:41-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577908-implementation-of-knuthmorrispratt-algorithm/ <p style="color: grey"> C recipe 577908 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/kmp/">kmp</a>, <a href="/recipes/tags/matching/">matching</a>, <a href="/recipes/tags/string/">string</a>). Revision 2. </p> <p>Knuth–Morris–Pratt (KMP) is a linear time string matching algorithm.</p> <p><a href="http://en.wikipedia.org/wiki/Knuth%25E2%2580%2593Morris%25E2%2580%2593Pratt_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm</a></p> <p>This recipe gives a C-version implementation.</p> Search sequences for sub-sequence (Python) 2011-08-19T05:17:00-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577850-search-sequences-for-sub-sequence/ <p style="color: grey"> Python recipe 577850 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/searching/">searching</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/sublist/">sublist</a>, <a href="/recipes/tags/substring/">substring</a>). </p> <p>The list and tuple index() method and <code>in</code> operator test for element containment, unlike similar tests for strings, which checks for sub-strings:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; "12" in "0123" True &gt;&gt;&gt; [1, 2] in [0, 1, 2, 3] False </code></pre> <p>These two functions, search and rsearch, act like str.find() except they operate on any arbitrary sequence such as lists:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; search([1, "a", "b", 2, 3], ["b", 2]) 2 </code></pre> get all possible combinations of characters given a string (Python) 2011-08-15T04:15:42-07:00Yanghttp://code.activestate.com/recipes/users/4178968/http://code.activestate.com/recipes/577842-get-all-possible-combinations-of-characters-given-/ <p style="color: grey"> Python recipe 577842 by <a href="/recipes/users/4178968/">Yang</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/permutation/">permutation</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>This will give a result that is more than a permutation, but all possible combinations. An example is when input is 'abc', the output would be: a,ab,abc,ac,acb,b,ba,bac,bc,bca,c,ca,cab,cb,cba</p> Split Strings w/ Multiple Separators (Python) 2011-03-20T20:33:13-07:00Kenneth Reitzhttp://code.activestate.com/recipes/users/4177394/http://code.activestate.com/recipes/577616-split-strings-w-multiple-separators/ <p style="color: grey"> Python recipe 577616 by <a href="/recipes/users/4177394/">Kenneth Reitz</a> (<a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/string_parsing/">string_parsing</a>). Revision 5. </p> <p>Splits strings with multiple separators instead of one (e.g. <code>str.split()</code>).</p> C# String.Replace with a char array (Java) 2010-12-24T09:58:35-08:00John Hurlimanhttp://code.activestate.com/recipes/users/4174599/http://code.activestate.com/recipes/577516-c-stringreplace-with-a-char-array/ <p style="color: grey"> Java recipe 577516 by <a href="/recipes/users/4174599/">John Hurliman</a> (<a href="/recipes/tags/csharp/">csharp</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>Takes a string and replaces all characters matching a given array with a given string</p> Auto Named Decriptors (Python) 2010-10-19T22:55:16-07:00Aloys Baillethttp://code.activestate.com/recipes/users/4175355/http://code.activestate.com/recipes/577426-auto-named-decriptors/ <p style="color: grey"> Python recipe 577426 by <a href="/recipes/users/4175355/">Aloys Baillet</a> (<a href="/recipes/tags/automatically/">automatically</a>, <a href="/recipes/tags/constants/">constants</a>, <a href="/recipes/tags/descriptors/">descriptors</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/string/">string</a>). Revision 3. </p> <p>Using named Descriptors? Tired of duplicating the name of the instance in a string? A small metaclass can solve this.</p> lreplace() and rreplace(): Replace the beginning and ends of a strings (Python) 2010-06-04T02:18:48-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/577252-lreplace-and-rreplace-replace-the-beginning-and-en/ <p style="color: grey"> Python recipe 577252 by <a href="/recipes/users/4169722/">Dan McDougall</a> (<a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>Python newbies will often make the following mistake (I certainly have =):</p> <pre class="prettyprint"><code>&gt;&gt;&gt; test = """this is a test: ... tis the season for mistakes.""" &gt;&gt;&gt; for line in test.split('\n'): ... print line.lstrip('this') ... is a test the season for mistakes. </code></pre> <p>The mistake is assuming that lstrip() (or rstrip()) strips a string (whole) when it actually strips all of the provided characters in the given string. Python actually comes with no function to strip a string from the left-hand or right-hand side of a string so I wrote this (very simple) recipe to solve that problem. Here's the usage:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; test = """this is a test: ... tis the season for mistakes.""" &gt;&gt;&gt; for line in test.split('\n'): ... print lreplace('this', '', line) ... is a test tis the season for mistakes. </code></pre> <p>I really wish Python had these functions built into the string object. I think it would be a useful addition to the standard library. It would also be nicer to type this:</p> <pre class="prettyprint"><code>line.lreplace('this', '') </code></pre> <p>Instead of this:</p> <pre class="prettyprint"><code>lreplace('this','',line) </code></pre> partitionall() - partition until the end (Python) 2010-03-01T15:27:07-08:00Shashwat Anandhttp://code.activestate.com/recipes/users/4172995/http://code.activestate.com/recipes/577060-partitionall-partition-until-the-end/ <p style="color: grey"> Python recipe 577060 by <a href="/recipes/users/4172995/">Shashwat Anand</a> (<a href="/recipes/tags/string/">string</a>). Revision 4. </p> <p>Search for the separator sep in string s, and return the part before it, the separator itself, and the part after it continuously untill all the seperator finishes. If the separator is not found, return string s. It works like split() combined with partition()</p>