String s = "Hello"; for (int i=0;i <= 4;i++) System.out.println(s.charAt(i));Note that in this example we knew how many characters were in the string. However, we will not always know this. For this reason, there is a method associated with strings called length.
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(stream);
String s = keyboard.readLine();
System.out.println("You entered " + s.length() + " characters.");
Another useful method is endsWith.
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(stream);
System.out.println("Please enter a binary number");
String s = keyboard.readLine();
if (s.endsWith("1"))
System.out.println("The decimal equivalent of " + s + " is odd.");
else
System.out.println("The decimal equivalent of " + s + " is even.");
String s1,s2;
if (s1.equals(s2))
System.out.println("s1 and s2 are equal");
else
System.out.println("s1 and s2 are not equal");
Another important operations related to strings is the ability to extract part of the string. We do this with the
substring method. This is an overloaded method.