Skip to content

Commit aa58ca7

Browse files
author
Vinh Nguyen
committed
- improve formatting
- add notes
1 parent 2613655 commit aa58ca7

File tree

2 files changed

+158
-17
lines changed

2 files changed

+158
-17
lines changed

common-validator/docs/verhoeff.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ Apache Commons provides the class `VerhoeffCheckDigit`.
1515
```java
1616
@Test
1717
public void calculateChecksum()throws CheckDigitException{
18-
// given
19-
String companyCode="90368002";
20-
String expectedChecksum="3";
21-
22-
// when
23-
String actual=VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.calculate(companyCode);
24-
logger.info("Checksum for {} is {}",companyCode,actual);
25-
//then
26-
assertEquals(expectedChecksum,actual);
27-
}
18+
// given
19+
String companyCode="90368002";
20+
String expectedChecksum="3";
21+
22+
// when
23+
String actual=VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.calculate(companyCode);
24+
logger.info("Checksum for {} is {}",companyCode,actual);
25+
//then
26+
assertEquals(expectedChecksum,actual);
27+
}
2828
```
2929

3030
## Validate a code
3131

3232
```java
3333
@Test
3434
public void validateChecksum(){
35-
// given
36-
String code="903680023";
37-
String faultyCode="1256369654";
38-
// when && then
39-
assertTrue(VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.isValid(code));
40-
assertFalse(VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.isValid(faultyCode));
41-
}
35+
// given
36+
String code="903680023";
37+
String faultyCode="1256369654";
38+
// when && then
39+
assertTrue(VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.isValid(code));
40+
assertFalse(VerhoeffCheckDigit.VERHOEFF_CHECK_DIGIT.isValid(faultyCode));
41+
}
4242
```

docs/basics/lang/String.md

+141
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,147 @@ Back to [Main](../../../README.md) ⇒ [Base](../index.md)
44

55
* [Java 11 Doc](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html)
66

7+
## Comparing Strings
8+
9+
```java
10+
String s1 = "a";
11+
String s2 = "A";
12+
String s3 = "B";
13+
14+
// Check if identical
15+
boolean b = s1.equals(s2); // false
16+
17+
// Check if identical ignoring case
18+
b = s1.equalsIgnoreCase(s2); // true
19+
20+
// Check order of two strings
21+
int i = s1.compareTo(s2); // 32; lowercase follows uppercase
22+
if (i < 0) {
23+
// s1 precedes s2
24+
} else if (i > 0) {
25+
// s1 follows s2
26+
} else {
27+
// s1 equals s2
28+
}
29+
30+
// Check order of two strings ignoring case
31+
i = s1.compareToIgnoreCase(s3); // -1
32+
if (i < 0) {
33+
// s1 precedes s3
34+
} else if (i > 0) {
35+
// s1 follows s3
36+
} else {
37+
// s1 equals s3
38+
}
39+
40+
// A string can also be compared with a StringBuffer;
41+
// see Constructing a String
42+
StringBuffer sbuf = new StringBuffer("a");
43+
b = s1.contentEquals(sbuf); // true
44+
```
45+
46+
## Constructing a String
47+
48+
If you are constructing a string with several appends, it may be more efficient to construct it using a `StringBuffer` (synchronized), `StringBuilder` (non-synchronized) and then convert it to an immutable `String` object.
49+
50+
```java
51+
StringBuffer buf = new StringBuffer("Java");
52+
53+
// Append
54+
buf.append(" Almanac v1/"); // Java Almanac v1/
55+
buf.append(3); // Java Almanac v1/3
56+
57+
// Set
58+
int index = 15;
59+
buf.setCharAt(index, '.'); // Java Almanac v1.3
60+
61+
// Insert
62+
index = 5;
63+
buf.insert(index, "Developers ");// Java Developers Almanac v1.3
64+
65+
// Replace
66+
int start = 27;
67+
int end = 28;
68+
buf.replace(start, end, "4"); // Java Developers Almanac v1.4
69+
70+
// Delete
71+
start = 24;
72+
end = 25;
73+
buf.delete(start, end); // Java Developers Almanac 1.4
74+
75+
// Convert to string
76+
String s = buf.toString();
77+
```
78+
79+
## Converting a Primitive Type Value to a String
80+
81+
There are two ways to convert a primitive type value into a string.
82+
The explicit way is to call `String.valueOf()`.
83+
The implicit way is to use the string concatenation operator `+`.
84+
85+
```java
86+
// Use String.valueOf()
87+
String s = String.valueOf(true); // true
88+
s = String.valueOf((byte)0x12); // 18
89+
s = String.valueOf((byte)0xFF); // -1
90+
s = String.valueOf('a'); // a
91+
s = String.valueOf((short)123); // 123
92+
s = String.valueOf(123); // 123
93+
s = String.valueOf(123L); // 123
94+
s = String.valueOf(1.23F); // 1.23
95+
s = String.valueOf(1.23D); // 1.23
96+
97+
// Use +
98+
s = ""+true; // true
99+
s = ""+((byte)0x12); // 18
100+
s = ""+((byte)0xFF); // -1
101+
s = ""+'a'; // a
102+
s = ""+((short)123); // 123
103+
s = ""+123; // 123
104+
s = ""+123L; // 123
105+
s = ""+1.23F; // 1.23
106+
s = ""+1.23D; // 1.23
107+
```
108+
109+
## Converting a String to Upper or Lower Case
110+
111+
```java
112+
// Convert to upper case
113+
String upper = string.toUpperCase();
114+
115+
// Convert to lower case
116+
String lower = string.toLowerCase();
117+
```
118+
119+
## Converting Between Unicode and UTF-8
120+
121+
```java
122+
try {
123+
// Convert from Unicode to UTF-8
124+
String string = "abc\u5639\u563b";
125+
byte[] utf8 = string.getBytes("UTF-8");
126+
127+
// Convert from UTF-8 to Unicode
128+
string = new String(utf8, "UTF-8");
129+
} catch (UnsupportedEncodingException e) {
130+
131+
}
132+
```
133+
134+
## Determining a Character's Unicode Block
135+
136+
## Determining If a String Contains a Substring
137+
138+
## Determining If a String Is a Legal Java Identifier
139+
140+
## Getting a Substring from a String
141+
142+
## Replacing Characters in a String
143+
144+
## Replacing Substrings in a String
145+
146+
## Searching a String for a Character or a Substring
147+
7148
## Split Contents
8149

9150
Two methods are available

0 commit comments

Comments
 (0)