Tuesday, February 18, 2020

Java Strings Part 3

charAt() method retrieves a given character by index number (starting at zero)

public class charat {

    public static void main(String[] av) {
        String a = "Hello world";
        for (int i = 0; i < a.length(); i++) {
            System.out.println("Char " + i + " is " + a.charAt(i));
        }
        //Char 0 is H
        //Char 1 is e
        //Char 2 is l
        //Char 3 is l
        //Char 4 is o
        //Char 5 is  
        //Char 6 is w
        //Char 7 is o
        //Char 8 is r
        //Char 9 is l
        //Char 10 is d

        String s = "Hello world";
        //Does not work, in Java 7
        for (char ch : s.toCharArray()) {
            System.out.println(ch);
        }
        //H
        //e
        //l
        //l
        //o
        // 
        //w
        //o
        //r
        //l
        //d

    }
}

Java Strings Part 2
Java Strings Part 1

Thursday, February 13, 2020

Java Strings Part 2

String pieces (back) together
Use the + operator. The compiler implicitly constructs a StringBuilder for you and uses its append() methods
StringBuffer -> synchronized
StringBuilder -> not synchronized -> faster and preferable for single-threaded use. (not threadsafe)
AbstractStringBuilder -> parent of both

Example

public class StringBuilderExample {

    public static void main(String[] argv) {
        String s1 = "Hello" + ", " + "World";
        System.out.println(s1); //Hello, World
        
        StringBuilder sb2 = new StringBuilder();
        sb2.append("Hello");
        sb2.append(',');
        sb2.append(' ');
        sb2.append("World");
        String s2 = sb2.toString();
        System.out.println(s2); //Hello, World

        System.out.println(new StringBuilder().append("Hello").append(',').append(' ').append("World"));
        //Hello, World
    }
}


List of items into a comma-separated list

import java.util.StringTokenizer;

public class StringBuilderComma {
    public static final String SAMPLE_STRING = "HELLO WORLD OF JAVA";
    public static void main(String[] argv) {
        StringBuilder sb1 = new StringBuilder();
        for (String word : SAMPLE_STRING.split(" ")) {
            if (sb1.length() > 0) {
                sb1.append(", ");
            }
            sb1.append(word);
        }
        System.out.println(sb1); //HELLO, WORLD, OF, JAVA

        
        StringTokenizer st = new StringTokenizer(SAMPLE_STRING);
        StringBuilder sb2 = new StringBuilder();
        while (st.hasMoreElements()) {
            sb2.append(st.nextToken());
            if (st.hasMoreElements()) {
                sb2.append(", ");
            }
        }
        System.out.println(sb2); //HELLO, WORLD, OF, JAVA

    }
}
Java Strings Part 3

Java Strings Part 1

String object, once constructed, is immutable
String s = "Hello" + yourName;
The contents of the particular object that reference variable s refers to can never be changed.
If you need to change characters within a String, you should instead create a StringBuilder (mutable), manipulate the StringBuilder, then convert that to String using toString()
String is a fundamental type in Java. 
The class is marked final so it cannot be subclassed. So you can’t declare your own String subclass
public class getInfo extends java.lang.String { //<-ERROR. Cannot inherit from final String

Break a String
substring() method constructs a new String Object
substring method is overloaded: both forms require a starting index (zero-based) and optional ending index

Example
public class SubStringExample {

    public static void main(String[] av) {
        String a = "Good Morning.";
        System.out.println(a);      //Good Morning.
        String b = a.substring(5);  
        System.out.println(b);      //Morning.
        String c = a.substring(5, 7);
        System.out.println(c);      //Mo
        String d = a.substring(5, a.length());
        System.out.println(d);      //Morning.
    }
}


Break a String into Words
StringTokenizer and use methods hasMoreTokens() and nextToken()
StringTokenizer breaks the String into tokens

Example
import java.util.StringTokenizer;

/**
 *
 * @author AlejandroDavilaFlore
 */
public class strTok {

    public static void main(String[] av) {
        StringTokenizer st = new StringTokenizer("Hello World of Java");
        while (st.hasMoreTokens()) {
            System.out.println("Token: " + st.nextToken());
        }
        //Token: Hello
        //Token: World
        //Token: of
        //Token: Java

        //second string ", |"
        StringTokenizer st2 = new StringTokenizer("Hello, World|of|Java", ", |");
        while (st2.hasMoreElements()) {
            System.out.println("Token: " + st2.nextElement());
        }
        //Token: Hello
        //Token: World
        //Token: of
        //Token: Java
        
        //second string ", |"   
        //third string true, see the delimiters as tokens.
        StringTokenizer st3 = new StringTokenizer("Hello, World|of|Java", ", |", true);
        while (st3.hasMoreElements()) {
            System.out.println("Token: " + st3.nextElement());
        }
        //Token: Hello
        //Token: ,
        //Token:  
        //Token: World
        //Token: |
        //Token: of
        //Token: |
        //Token: Java

    }
}

Java Strings Part 2