Skip to content

Instantly share code, notes, and snippets.

View luisenriquecorona's full-sized avatar
😎
I may be slow to respond.

TextKi JS luisenriquecorona

😎
I may be slow to respond.
View GitHub Profile
@luisenriquecorona
luisenriquecorona / String.py
Created April 18, 2019 17:59
In fact, Python itself sometimes uses this doubling scheme when it prints strings with embedded backslashes
>>> 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
@luisenriquecorona
luisenriquecorona / StringAlign.java
Created April 18, 2019 23:12
Is the code for the StringAlign class. Note that this class extends a class called Format. In the package java.text, there is a series of Format classes that all have at least one method called format( ). It is thus in a family with numerous other for- matters, such as DateFormat, NumberFormat, and others, that we’ll meet in upcoming chapters.
/** 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';
@luisenriquecorona
luisenriquecorona / UnicodeChars.java
Created April 18, 2019 23:24
Conversion between Unicode characters and Strings
/**
* 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
@luisenriquecorona
luisenriquecorona / StringReverse.java
Created April 18, 2019 23:39
Adds each one to a Stack , then processes the whole lot in LIFO order, which reverses the order.
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(' ');
@luisenriquecorona
luisenriquecorona / EnTab.java
Last active April 18, 2019 23:46
The code also has several Debug printouts
/**
* 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) */
@luisenriquecorona
luisenriquecorona / TOC.js
Created April 18, 2019 23:59
An automatically generated table of contents Create a table of contents for a document
/**
* 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
@luisenriquecorona
luisenriquecorona / Querying.js
Created April 19, 2019 00:28
Querying the scrollbar positions of a window
// 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};
@luisenriquecorona
luisenriquecorona / Knight.py
Created April 19, 2019 00:46
Other string methods have more focused roles—for example, to strip off whitespace at the end of a line of text, perform case conversions, test content, and test for a substring at the end or front
>>> 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')
@luisenriquecorona
luisenriquecorona / Switch.js
Created April 19, 2019 23:47
What is the weather like
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:
@luisenriquecorona
luisenriquecorona / Formatting.py
Created April 20, 2019 00:02
String Formatting Method Calls. Formatting Method Basics.
>>> 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