Spaces:
Running
Running
Create 10a Strings
Browse files
Week 2: Methods, strings and lists/10a Strings
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Java has variables of reference type.
|
| 2 |
+
In a reference type variable, the variable is placed in reference to the actual value.
|
| 3 |
+
|
| 4 |
+
String b = "bye!";
|
| 5 |
+
- immutable
|
| 6 |
+
- but can point to another string
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
================
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
String name = "Jack Java";
|
| 13 |
+
String address = "Java rd. " + "64" + ", 12345, Javatown";
|
| 14 |
+
String phonenum = "040-" + (12345 * 23456); //multiply, then concat as strings
|
| 15 |
+
System.out.println(name);
|
| 16 |
+
System.out.println(address);
|
| 17 |
+
System.out.println(phonenum);
|
| 18 |
+
|
| 19 |
+
Program outputs:
|
| 20 |
+
Jack Java
|
| 21 |
+
Java rd. 64, 12345, Javatown
|
| 22 |
+
040-289564320
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
================
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
String length - .length()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
String first = "Hey";
|
| 35 |
+
System.out.println(first.length());
|
| 36 |
+
|
| 37 |
+
System.out.println("Hello everyone".length());
|
| 38 |
+
|
| 39 |
+
int length = (first + "!!!").length();
|
| 40 |
+
System.out.println(length);
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
Program outputs:
|
| 44 |
+
3
|
| 45 |
+
14
|
| 46 |
+
6
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
================
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
single characters - charAt(i)
|
| 54 |
+
|
| 55 |
+
Scanner reader = new Scanner(System.in);
|
| 56 |
+
System.out.print("Give a string: ");
|
| 57 |
+
String str = reader.nextLine();
|
| 58 |
+
|
| 59 |
+
for (int i=0; i<str.length(); i++) {
|
| 60 |
+
System.out.println(str.charAt(i));
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
Program outputs:
|
| 65 |
+
Give a string: Test
|
| 66 |
+
T
|
| 67 |
+
e
|
| 68 |
+
s
|
| 69 |
+
t
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
// returns a 'character' - which is a number type
|
| 79 |
+
char first = 'O';
|
| 80 |
+
char second = 'k';
|
| 81 |
+
System.out.println(first + second);
|
| 82 |
+
|
| 83 |
+
Program outputs:
|
| 84 |
+
186
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
// A character can be converted into a string
|
| 89 |
+
// 1 either by using the method toString in the Character class
|
| 90 |
+
char cr = 'X';
|
| 91 |
+
String str = Character.toString(cr);
|
| 92 |
+
|
| 93 |
+
Program outputs:
|
| 94 |
+
X
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
// 2 or usually more easily by CONCATenating a character with an EMPTY STRING:
|
| 98 |
+
char first = 'O';
|
| 99 |
+
char second = 'k';
|
| 100 |
+
System.out.println("" + first + second);
|
| 101 |
+
|
| 102 |
+
Program outputs:
|
| 103 |
+
Ok
|
| 104 |
+
|
| 105 |
+
================
|