Sunday, August 26, 2012

SQL String functions

Some STRING functions in SQL:

select RIGHT(column, chars) from table;
select SUBSTRING_INDEX(column, char, which char (i.e, 1,2...)) from table;
select REVERSE(string);
select LTRIM(string);
select RTRIM(string);
select LENGTH(string);

Monday, July 30, 2012

Java Pitfalls Book


This book "Java™ Puzzlers: Traps, Pitfalls, and Corner Cases" gives insight into some Java "fails" that one needs to watch out for when developing.

It explains what is wrong with this piece of code (It doesn't print out AB as one would expect):

public class PrintingSomeLetters {
  public static void main(String[] args) {
    System.out.println('A' + 'B');
  }
}

Friday, June 22, 2012

Design Patterns Book

Tuesday, May 15, 2012

Java 5 autoboxing fail on equality check

  Double d1 = 25d;
  Double d2 = 25d;

  if (d1 == d2) {
            System.out.println("equal (==)");
  } else if (d1.equals(d2)) {
            System.out.println("equal (using equals() method)");
  }

The output is:
equal (using equals() method)

Wednesday, May 2, 2012

Reverse search for a previous command on Linux

Use CTRL-r to reverse search through a history of your previous commands executed in Linux e.g.
$(reverse-i-search)`':

Type in the text for a command you are searching for e.g. 'cat'
$(reverse-i-search)`cat': cat test.txt

In this case the last command executed with the substring 'cat' is 'cat test.txt'.

To exit the reverse search just use CTRL-g.

Thursday, April 26, 2012

Using SED to Append or Prepend characters

You can use sed to Append/Prepend characters to a String e.g.

We have a file (test.txt) below and we would like to Prepend each capital letter with a space:
$ cat test.txt
Hello.ThisIsATestFile.

$ sed "s/[A-Z]/ &/g" test.txt
Hello. This Is A Test File.

sed makes use of a special character "&" which implies the pattern found.

Similarly we could Append a sentence after the "Hello." string:
$ sed "s/Hello./&HowAreYou?/g" test.txt 
Hello.HowAreYou?ThisIsATestFile.