Repository navigation
Expand file tree
/
Copy pathStrings.java
More file actions
82 lines (63 loc) · 2.26 KB
/
Copy pathStrings.java
File metadata and controls
82 lines (63 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Strings are immutable in java, once built they cannot be changed
//If you want a different string, you must make a new one with that change.
//building strings many times is very slow and inefficient
String s = "blue";
String m = s + "moon";
s = s + m;
m = "low" + s;
//S = bluebluemoon, m = lowbluebluemoon
//So, it is recommended to use StringBuilder.
//mutable sequence of characters, but can be modified.
StringBuilder sb = new StringBuilder("hello");
sb.append("World") //result = hello World
.append(),
.insert(),
.charAt(),
.setCharAt(),
.toString()
//Difference between using StringBuilder and String
StringBuilder sb = new StringBuilder("start");
sb.insert(4,"le");
System.out.println(sb);
//With no StringBuilder
String s = "start";
s = s.substring(0,4) + "le" + s.substring(4);
System.out.println(s);
//reversing a string with Java:
public String reverse(String s) {
String ret = "";
for(int i = 0; i < s.length; i+= 1) {
ret = s.charAt(i) + ret; //this will go in reverse as the next character will appear before the previously placed ret character.
}
return ret;
}
//Char Building
//type char is primitive with single quotes:
"a" = String
//several methods:
.isLowerCase(ch) //returns boolean if ch is a letter 'a'
.isDigit(ch) //returns boolean if it's a number or not '1'
.toLowerCase(ch) //returns lowercase
.toUpperCase(ch) //returns uppercase
//using array as an indexed collection:
StorageResource //to collect strings
//useful, but limited.
//define array with []
int[] a = new int[256] //specifying the specific array storage size
for(int i = 0; i < a.length; i++) {
int val = a[i];
}
//Indexed collection of elements/values and it reads and writes via indexes
int[] x; //no storage, just a type
int[] x = new int[12]; //all initialized to zero, to say how many are stored
String[] s = new String[12]; //all initialized to null.
//read and write:
s[3] = "Hello"; //storing
x[2] = x[3] + 4; //access and reading an array location like this.
//storage allocated and doesn't change.
//arr.length - find the number of elements in an array
//equality
//Two types of equality:
//1) == : same exact object
//2) .equals() : same meaning
//Defined by class: default == `