/***************************************************************/ /* Prof. Dr. Carsten Vogt */ /* TH Koeln, Fakultaet IME */ /* http://www.nt.th-koeln.de/vogt */ /* */ /* Das Programm zeigt den Umgang mit Strings. (1. Beispiel) */ /***************************************************************/ public class Strings { public static void main(String args[]) { /* Deklaration und Initialisierung: */ String s1 = new String("Hallo"); String s2; s2 = s1; /* s1 und s2 zeigen jetzt auf dasselbe Stringobjekt */ System.out.println(); System.out.println("s1: " + s1); System.out.println("s2: " + s2); /* Verkettung von Strings */ s1 = s1 + new String(" Welt"); /* neues Stringobjekt entsteht */ System.out.println(); System.out.println("Nach Anhaengen:"); System.out.println("s1: " + s1); System.out.println("s2: " + s2); /* Stringlaenge: */ System.out.println(); System.out.println("s1.length(): " + s1.length()); System.out.println("s2.length(): " + s2.length()); /* Stringvergleiche: */ System.out.println(); System.out.println("s1.equals(s2): " + s1.equals(s2)); System.out.println("s1.compareTo(s2): " + s1.compareTo(s2)); System.out.println("s1.startsWith(\"hal\"): " + s1.startsWith("hal")); System.out.println("s1.startsWith(\"Hal\"): " + s1.startsWith("Hall")); System.out.println("s1.endsWith(\"elt\"): " + s1.endsWith("elt")); /* Suche in Strings: */ System.out.println(); System.out.println("s1.contains(\"llo\"): " + s1.contains("llo")); System.out.println("s1.contains(\"www\"): " + s1.contains("www")); System.out.println("s1.indexOf(\"llo\"): " + s1.indexOf("llo")); System.out.println("s1.indexOf(\"www\"): " + s1.indexOf("www")); /* Einzelzeichen im String und Teilstrings: */ System.out.println(); System.out.println("s1.charAt(2): " + s1.charAt(2)); System.out.println("s1.substring(2): " + s1.substring(2)); System.out.println("s1.substring(2,6): " + s1.substring(2,6)); /* Umwandlung in Gross- und Kleinbuchstaben: */ System.out.println(); System.out.println("s1.toUpperCase(): " + s1.toUpperCase()); System.out.println("s1.toLowerCase(): " + s1.toLowerCase()); /* Ersetzung von Zeichen: */ System.out.println(); System.out.println("s1.replace('l','r'): " + s1.replace('l','r')); /* Anhaengen: */ System.out.println(); System.out.println("s2.concat(\" Welt\"): " + s2.concat(" Welt")); } }