Java Strings: The Basics

One long section of code outlining how to achieve some basic string handling objectives in Java. Currently trying to get to grips with this language after spending far too many years concentrating on C++. Each technique is demonstrated with a code snippet. As with a lot of my other stuff, any new stuff I find useful will get added at a later date:

package stringdemos;
import java.util.*;

public class StringDemos 
{ 
    public static void main(String[] args) 
    {
        // Initialize string
        String greeting = "Hello world!";
        System.out.println( greeting );

        // String length
        int len = greeting.length();

        // Copy string into temporary character array
        char[] tempCharArray = new char[len];			
        for (int i = 0; i < len; i++) 
        {
            tempCharArray[i] = greeting.charAt(i);                  
        } 
        
        // Reverse an array of chars
        for (int j = 0; j < len / 2; j++) 
        {
            char temp = tempCharArray[ j ];        	
            tempCharArray[ j ] = tempCharArray[ len - j - 1 ];
            tempCharArray[ len - j - 1 ] = temp;
        }  
        System.out.println( tempCharArray );  
        
        // Concatenate strings
        String strCat = greeting.concat( " ");
        strCat = strCat + "Again";
        
        // Output formatting
        double v = 1.234;
        String fs = String.format( "\nValue = %.2f", v );
        System.out.println( fs ); 
        
        // Convert number to string and vice versa
        Float fv = Float.valueOf( "1.23");
        String fvStr = String.valueOf(( fv ));
        
        // Getting substrings
        String sub1 = greeting.substring( 0, 4 );
        String sub2 = greeting.substring( 4 );
                
        // Tokenizing strings eg whitespaces
        String[] tok = greeting.split(" ");
        
        // Remove any trailing and leading whitespaces
        String whiteSpace = "   remove white spaces, beginning and end   ";
        whiteSpace.trim();
        
        // Convert upper/lower cases
        String strCase = greeting.toLowerCase();
        strCase = strCase.toUpperCase();
        
        // Find characters and substrings in strings
        String txt1 = "Hello";
        String txt2 = "Hello";
        String txt3 = "hello";
        int comp =  txt1.compareTo(txt2);
        comp = txt1.compareTo(txt3);
        comp = txt1.compareToIgnoreCase(txt3);
        
        // Finding substrings within strings
        String strFind = "The cat sat on the mat";
        int index1 = strFind.indexOf("at");              // First occurence
        int index2 = strFind.indexOf("at", index1 + 1);  // Next occurence
        int index3 = strFind.indexOf("at", index2 + 1);  // Next occurence  
        
        // Sorting string arrays
        String[] txt = { "I", "walk", "the", "line" };
        List<String> list = Arrays.asList(txt);
        Collections.sort(list);
        System.out.println(list);
    }
}

Leave a Reply