This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> path = r'C:\new\text.dat' | |
>>> path | |
'C:\\new\\text.dat' | |
>>> print(path) | |
C:\new\text.dat | |
>>> len(path) 15 | |
# Show as Python code # User-friendly format # String length |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Bare-minimum String formatter (string aligner). */ | |
public class StringAlign extends Format { | |
/* Constant for left justification. */ | |
public static final int JUST_LEFT = 'l'; | |
/* Constant for centering. */ | |
public static final int JUST_CENTRE = 'c'; | |
/* Centering Constant, for those who spell "centre" the American way. */ | |
public static final int JUST_CENTER = JUST_CENTRE; | |
/** Constant for right-justified Strings. */ | |
public static final int JUST_RIGHT = 'r'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Conversion between Unicode characters and Strings | |
*/ | |
public class UnicodeChars { | |
public static void main(String[] argv) { | |
StringBuffer b = new StringBuffer( ); | |
for (char c = 'a'; c<'d'; c++) { | |
b.append(c); | |
} | |
b.append('\u00a5'); // Japanese Yen symbol |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String s = "Father Charles Goes Down And Ends Battle"; | |
// Put it in the stack frontward | |
Stack myStack = new Stack( ); | |
StringTokenizer st = new StringTokenizer(s); | |
while (st.hasMoreTokens( )) myStack.push(st.nextElement( )); | |
// Print the stack backward | |
System.out.print('"' + s + '"' + " backwards by word is:\n\t\""); | |
while (!myStack.empty( )) { | |
System.out.print(myStack.pop( )); | |
System.out.print(' '); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* EnTab: replace blanks by tabs and blanks. Transmuted from K&R Software Tools | |
* book into C. Transmuted again, years later, into Java. Totally rewritten to | |
* be line-at-a-time instead of char-at-a-time. | |
* | |
* @author Ian F. Darwin, http://www.darwinsys.com/ | |
* @version $Id: ch03,v 1.3 2004/05/04 18:03:14 ian Exp $ | |
*/ | |
public class EnTab { | |
/** The Tabs (tab logic handler) */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* TOC.js: create a table of contents for a document. | |
* | |
* This module registers an anonymous function that runs automatically | |
* when the document finishes loading. When it runs, the function first | |
* looks for a document element with an id of "TOC". If there is no | |
* such element it creates one at the start of the document. | |
* | |
* Next, the function finds all <h1> through <h6> tags, treats them as | |
* section titles, and creates a table of contents within the TOC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Return the current scrollbar offsets as the x and y properties of an object | |
function getScrollOffsets(w) { | |
// Use the specified window or the current window if no argument | |
w = w || window; | |
// This works for all browsers except IE versions 8 and before | |
if (w.pageXOffset != null) return {x: w.pageXOffset, y:w.pageYOffset}; | |
// For IE (or any browser) in Standards mode | |
var d = w.document; | |
if (document.compatMode == "CSS1Compat") | |
return {x:d.documentElement.scrollLeft, y:d.documentElement.scrollTop}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> line = "The knights who say Ni!\n" | |
>>> line.rstrip() | |
'The knights who say Ni!' | |
>>> line.upper() | |
'THE KNIGHTS WHO SAY NI!\n' | |
>>> line.isalpha() | |
False | |
>>> line.endswith('Ni!\n') | |
True | |
>>> line.startswith('The') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch (prompt("What is the weather like?")) { | |
case "rainy": | |
console.log("Remember to bring an umbrella."); | |
break; | |
case "sunny": | |
console.log("Dress lightly."); | |
case "cloudy": | |
console.log("Go outside."); | |
break; | |
default: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> template = '{0}, {1} and {2}' # By position | |
>>> template.format('spam', 'ham', 'eggs') | |
'spam, ham and eggs' | |
>>> template = '{motto}, {pork} and {food}' # By keyword | |
>>> template.format(motto='spam', pork='ham', food='eggs') | |
'spam, ham and eggs' | |
>>> template = '{motto}, {0} and {food}' # By both | |
>>> template.format('ham', motto='spam', food='eggs') | |
'spam, ham and eggs' | |
>>> template = '{}, {} and {}' # By relative position |