Java: Join String Array

Java has a string split function, but it does not have a combine/join/unsplit string function. Below is an example modified from the StringUtils library.

package test;

public class Join {
    public static void main(String[] args) {
        String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};
        System.out.println(join(toppings, ";"));
        System.out.println(join(toppings, null));
    }

    public static String join(final String[] array, final String separator) {
        if (array == null) {
            return null;
        }
        final StringBuilder buf = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                buf.append(separator==null ? "" : separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }
}

Output:

Cheese;Pepperoni;Black Olives
CheesePepperoniBlack Olives

Java PadLeft/PadRight

Java left pad/right pad string functions. These’re also equivalent to C# and VB.Net PadLeft and PadRight methods.

package test;

public class Pad {
    public static String lpad(String str, int n, CharSequence c) {
        return (new String(new char[n-str.length()])).replace("\0", c) + str;
    }

    public static String rpad(String str, int n, CharSequence c) {
        return str + (new String(new char[n-str.length()])).replace("\0", c);
    }
    
    public static void main(String[] args) {
        System.out.println(lpad("astring", 10, "0"));
        System.out.println(rpad("astring", 10, "0"));
    }
}

Output:

000astring
astring000

Java: Read/Write File

Below is the source code for reading and writing text files. Unless the file size is horrendously huge, this utility should suffice.  The source code works for all versions of Java.

package test;

import java.io.*;

public class FileReadWrite {

    public static void main(String[] arg) throws IOException {
        writeFile("C:\\temp\\b.txt", "Initial contents of this file.");
        
        String outfile = readFile("C:\\temp\\b.txt");
        System.out.println("Reading file contents: "
                + outfile);

        writeFile("C:\\temp\\b.txt", "This file has been overwritten...");

        outfile = readFile("C:\\temp\\b.txt");
        System.out.println("Reading new file contents: "
                + outfile);
    }

    public static String readFile(String filename) {
        StringBuilder contents = new StringBuilder();
        try {
            File aFile = new File(filename);
            BufferedReader input = new BufferedReader(new FileReader(aFile));
            try {
                String line = null;
                while ((line = input.readLine()) != null) {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return contents.toString();
    }

    public static void writeFile(String filename, String aContents) {
        try {
            File aFile = new File(filename);
            Writer output = new BufferedWriter(new FileWriter(aFile));
            output.write(aContents);
            output.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Output:
Reading file contents: Initial contents of this file.

Reading new file contents: This file has been overwritten…

C# 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 C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    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);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            String str1 = "left string";
            Console.WriteLine("Test left str: " + str1);
            Console.WriteLine("Test left str: " + Test.left(str1, 4));

            String str2 = "right string";
            Console.WriteLine("Test right str: " + str2);
            Console.WriteLine("Test right str: " + Test.right(str2, 6));

            String str3 = "mid string";
            Console.WriteLine("Test mid str: " + str3);
            Console.WriteLine("Test mid str: " + Test.mid(str3, 2, 6));

            Console.WriteLine("Test mid str: " + str3);
            Console.WriteLine("Test mid str: " + Test.mid(str3, 5));
            Console.ReadKey();
        }
    }
}

Output:
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 stri
Test mid str: mid string
Test mid str: string

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));
    }
}

Output:
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

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 investexcel.net.  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.