Java Left / Right / Mid String Functions (as in Visual Basic.Net)

Visual Basic’s left, right, mid functions are nifty tricks that can be easily implemented in Java.

package test;

public class Test {
    public static String left(String input, int len) {
        return input.substring(0, len);
    public static String right(String input, int len) {
        return input.substring(input.length() - len);

    public static String mid(String input, int index, int len) {
        return input.substring(index - 1, index + len - 1);

    public static String mid(String input, int index) {
        return input.substring(index - 1);
    public static void main(String[] args) {
        String str1 = "left string";
        System.out.println("Test left str: " + str1);
        System.out.println("Test left str: " + left(str1, 4));    

        String str2 = "right string";
        System.out.println("Test right str: " + str2);
        System.out.println("Test right str: " + right(str2, 6));    

        String str3 = "mid string";
        System.out.println("Test mid str: " + str3);
        System.out.println("Test mid str: " + mid(str3, 2, 6));    

        System.out.println("Test mid str: " + str3);
        System.out.println("Test mid str: " + mid(str3, 5));

Test left str: left string
Test left str: left
Test right str: right string
Test right str: string
Test mid str: mid string
Test mid str: id str
Test mid str: mid string
Test mid str: string

Why The Average Investor’s Investment Return Is So Low

According to the latest 2014 release of Dalbar’s Quantitative Analysis of Investor Behavior (QAIB), the average investor in a blend of equities and fixed-income mutual funds has garnered only a 2.6% net annualized rate of return for the 10-year time period ending Dec. 31, 2013.

The same average investor hasn’t fared any better over longer time frames.  The 20-year annualized return comes in at 2.5%, while the 30-year annualized rate is just 1.9%. Wow!

The above is just one of the many studies that revealed the plight of Joe investor.  While institutional investors are reaping market returns; individual investors, because of agency and behavioral issues etc, suffer mediocre growth in their assets.  Similar studies in Singapore based on our CPF accounts exposed comparable dilemma.

The Investor’s Gift Exchange Paradox

One fine day, an angel descends from heaven and picks you to offer a gift.

She displays two envelopes in front of her and tells you that the two envelopes both contain money.  However, she cannot tell you how much money is contained within the two sealed envelopes; all she can say is that one envelope contains 10,000 times the amount of money the other envelope contains.  To put that in another way, one envelope contains 1/10,000 of what the other envelope contains.  Which envelope you pick depends entirely upon your luck.

So you close your eyes and pick one of them.  You discover a $100 bill inside the envelope.  Now, the angel makes another offer to you.  “Would you like to exchange your envelope with the $100 bill for the other envelope which I’ve previously offered you but you’ve not chosen?”

Now, some of you might get incredulous.  You might ask, “Wouldn’t the two envelopes be the same?  If I had chosen the other envelope, wouldn’t I now be offered the current envelope?”

However, the answer is not that simple.  First of all, I need you to think like a truly rational, risk-taking investor.  Take a moment to ponder upon the question before you look at the answer.

The answer is that you should exchange the $100 bill for the other envelope.

You must remember that one envelope contains 10,000 times the amount of money that the other envelope contains.  So, the other envelope will contain either a meager 1 cent ($100/10000) or a whopping $1 million ($100X10000).  As an enterprising investor, surely you would risk a 50% chance of losing only nearly all of $100 or a 50% chance of winning the million-dollar jackpot.  In mathematical terms, the expected outcome of the exchange, E(x), is ($1,000,000+$0.01)/2 = $500,000.005.  This is a tremendously good offer by the benevolent angel.

Now, the question – do you see the contradictory paradox in the above passage?

Downloading Forex Prices into Excel Spreadsheet

You can download historical foreign exchange data into a Microsoft Excel spreadsheet using the following XLS.

The spreadsheet provided above is an improvement over the excellent VBA code provided by the website  The following two features are added:

  • Combo boxes for the currency pair to make finding currencies simple and easy.
  • Date pickers for the to and from date ranges.

If you think either the combo boxes or the date pickers are a hassle to deal with and only want one of them, you can go to the developer tab, enable design mode, and delete them.  The VBA code will work just fine without them.

Valuations of the Various Stock Asset Classes

Benjamin Graham of value investing fame wrote in the book The Intelligent Investor, “Current price should not be more than 1.5 times the book value last reported.  However, a multiplier of earnings below 15 could justify a correspondingly higher multiplier of assets.  As a rule of thumb, we suggest that the product of the multiplier times the ratio of price to book value should not exceed 22.5 (This figure corresponds to 15 times earnings and 1.5 times book value.  It would admit an issue selling at only 9 times earnings and 2.5 times asset value, etc.)”

I’m of the opinion that Benjamin’s writings are pretty sage from the perspective of a value investor.  Let’s take a look at the prices of worldwide stocks in the various asset classes based on Mr. Graham’s sayings.  Since Vanguard publishes the two parameters for all of its stocks’ ETF, I shall use this fund company info for this purpose.  As listed on its website:

Asset class P/E P/B P/E * P/B
Global ex-US REIT 10.5 1.1 11.6
Emerging 13.9 1.7 23.6
US Financial 17.5 1.4 24.5
All-world ex US 16.7 1.7 28.4
Small-cap value 21.4 1.8 38.5
All-world ex US small-cap 20.0 2.5 50.0
S&P 500 19.1 2.7 51.6
Small-cap 27.2 2.3 62.6
US IT 21.2 3.9 82.7
US REIT 57.9 2.2 127.4

From the table above, the most glaring contradiction must come from the polar opposite of US and ex-US REIT sectors.  Ex-US REIT is the most value-y while US REIT is priced like it will have tremendous growth ahead (which I don’t think will be likely to materialize).  Perhaps 10 years down the road, we’ll most likely see a profitable delta of ex-US REIT over US REIT, considering the pricing disparity.

Of all the asset classes listed above, only Global ex-US REIT is comfortably within the limits set by Mr. Graham.  Emerging markets and US financial are also close and worth considering to overweigh in one’s portfolio.

Overall, most of the world’s stock markets are overpriced.  Everything is expensive!

Expected Returns of the Straits Times Index ETF

The SPDR STI ETF is a buyable security that is representative of Singapore’s Straits Times Index.  The ETF has low costs and is priced at about 1/1,000 of the index.

In this article, I’ll attempt to estimate the expected returns of this ETF if held over the long term, assuming that the economics and valuations of the stock market in the future do not differ much compared to today.  The followings are the fundamentals of the ETF as of today:

Distribution Yield 2.74%
Price/Cash Flow 9.50
Price/Earnings 13.27
Number of Holdings 30
Price/Book Ratio 1.28
Weighted Average Market Cap SGD $26,991.30 M


At the current P/E ratio of 13.27, the ETF has an average valuation compared to the historical norms.  The estimated net asset value per share is about $3.2 with an expense ratio of 0.3%.

Expected returns (per share) = dividends – expenses + capital appreciation

The dividends per share are 2.74% of $3.2, which is equal $0.088.

The expenses per share are 0.3% of $3.2, which is equal $0.010

Since the price/earnings is 13.27 and price is $3.2, earnings per share equals 3.2/13.27 = $0.241.

After the ETF pays out a dividend of $0.088, it will add to itself a retain-earnings of $0.241-$0.088 = $0.153.  Assuming that the market prices retained-earnings at a ratio of 1.28 (just like today’s P/B ratio), the addition to book value of $0.153 will cause a capital appreciation of $0.153 * 1.28 = $0.196.

As per the equation above, expected returns (per share) = 0.088 – 0.010 + 0.196 = $0.274

Assuming that years down the road, P/E remains at around 13.27 and P/B around 1.28, in percentage terms, the expected annualized return will be 0.274/3.2 = 8.6%!  This is pretty close to the ETF’s performance of 8.39% since its inception in April 2002!