strings - // part of a String; it gives us chars // at...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.*; // for Scanner public class Strings { public static void main (String [] args) { // Two ways to create a String: String x = new String("Hello, world!"); String s = "Hello, world!"; System.out.println(x); // Example of the length() method int l = x.length(); System.out.print("x is " + l); System.out.println(" chars long"); Scanner sc = new Scanner(System.in); System.out.print("ENter an index: "); int index = sc.nextInt(); System.out.print("Char at index " + index); System.out.println(": " + x.charAt(index)); // substring() allows us to extract
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // part of a String; it gives us chars // at indices start up to but not including // end String part = x.substring(2, 8); System.out.println("substring: " + part); // To test two strings for equality, // use the equals() method: // // first.equals(second) // // which return true or false if (x.equals(s) == true) { System.out.println("They are the same"); } else { System.out.println("Different"); } // == only tesst immediate values. For // Strings, this means it compares // memory addresses System.out.println(x == s); }...
View Full Document

This note was uploaded on 03/29/2012 for the course CSE 110 taught by Professor Shaunakpawagi during the Spring '08 term at SUNY Stony Brook.

Ask a homework question - tutors are online