Skip to content

Commit b292b04

Browse files
committed
new
1 parent efd15a9 commit b292b04

4 files changed

Lines changed: 182 additions & 0 deletions

File tree

java-string/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
6+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
7+
<modelVersion>4.0.0</modelVersion>
8+
<groupId>com.mkyong</groupId>
9+
<artifactId>string</artifactId>
10+
<version>1.0</version>
11+
12+
<name>java-string</name>
13+
<url>https://mkyong.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>17</maven.compiler.source>
18+
<maven.compiler.target>17</maven.compiler.target>
19+
<java.version>17</java.version>
20+
<junit.version>5.4.0</junit.version>
21+
</properties>
22+
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-params</artifactId>
28+
<version>${junit.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
</dependencies>
33+
34+
<build>
35+
<finalName>java-basic</finalName>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>3.8.1</version>
41+
<configuration>
42+
<source>${java.version}</source>
43+
<target>${java.version}</target>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mkyong.string;
2+
3+
import java.nio.charset.StandardCharsets;
4+
5+
public class ConvertBytesToString {
6+
7+
public static void main(String[] args) {
8+
9+
// String to byte[]
10+
byte[] bytes = "mkyong".getBytes(StandardCharsets.UTF_8);
11+
12+
// byte[] to String
13+
String s = new String(bytes, StandardCharsets.UTF_8);
14+
15+
// mkyong
16+
System.out.println(s);
17+
18+
}
19+
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.mkyong.string;
2+
3+
import java.nio.charset.Charset;
4+
import java.nio.charset.StandardCharsets;
5+
import java.util.Base64;
6+
7+
public class ConvertStringToBytes {
8+
9+
public static void main(String[] args) {
10+
11+
String example = "This is an example";
12+
13+
// default charset, a bit dangerous
14+
byte[] output1 = example.getBytes();
15+
16+
// in old days, before java 1.7
17+
byte[] output2 = example.getBytes(Charset.forName("UTF-8"));
18+
19+
// the best , java 1.7+
20+
byte[] output3 = example.getBytes(StandardCharsets.UTF_8);
21+
22+
System.out.println("Text : " + example);
23+
24+
// base64 encode
25+
String base64encoded = Base64.getEncoder().encodeToString(output3);
26+
27+
System.out.println("Text [Base64] : " + base64encoded);
28+
29+
}
30+
31+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.mkyong.string;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.StringTokenizer;
13+
14+
public class StringTokenizerExample {
15+
16+
public static void main(String[] args) throws IOException {
17+
18+
StringTokenizer st = new StringTokenizer("1, 2, 3, 4, 5");
19+
while (st.hasMoreTokens()) {
20+
System.out.println(st.nextToken().trim());
21+
}
22+
23+
/*String line = "This is a String, split by, StringTokenizer example.";
24+
List<String> result = split(line, ",");
25+
for (String s : result) {
26+
System.out.println(s.trim());
27+
}*/
28+
29+
/*StringTokenizerExample obj = new StringTokenizerExample();
30+
List<Trend> trends = obj.readFile(Paths.get("C:\\test\\sample.csv"), "|");
31+
trends.forEach(System.out::println);*/
32+
}
33+
34+
public static List<String> split(String line, String delimiter) {
35+
List<String> result = new ArrayList<>();
36+
StringTokenizer st = new StringTokenizer(line, delimiter);
37+
while (st.hasMoreTokens()) {
38+
result.add(st.nextToken());
39+
}
40+
return result;
41+
}
42+
43+
public List<Trend> readFile(Path path, String delimiter) throws IOException {
44+
45+
List<Trend> result = new ArrayList<>();
46+
47+
try (BufferedReader br = new BufferedReader(new FileReader(path.toString()))) {
48+
49+
String line;
50+
while ((line = br.readLine()) != null) {
51+
StringTokenizer st = new StringTokenizer(line, delimiter);
52+
while (st.hasMoreTokens()) {
53+
Integer id = Integer.parseInt(st.nextToken().trim());
54+
Double index = Double.parseDouble(st.nextToken().trim());
55+
String desc = st.nextToken().trim();
56+
result.add(new Trend(id, index, desc));
57+
}
58+
}
59+
}
60+
return result;
61+
}
62+
63+
class Trend {
64+
private int id;
65+
private Double index;
66+
private String desc;
67+
68+
public Trend(int id, Double index, String desc) {
69+
this.id = id;
70+
this.index = index;
71+
this.desc = desc;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return "Trend{" +
77+
"id=" + id +
78+
", index=" + index +
79+
", desc='" + desc + '\'' +
80+
'}';
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)