Friday , October 18 2024

[Video] Using StringTokenizer in Java

Lâu lâu mới lại có một tutorial, hôm nay mình sẽ giới thiệu cho các bạn biết về StringTokenizer trong Java và cách sử dụng nó.Youtube:

ScreenCast:

Source code:

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
import java.util.StringTokenizer;

/**
* Chuan chuoi va cach su dung StringTokenizer
* @author vnitt.net
*/

public class ChuanChuoi {

private String ChuanChuoi(String input) {
String result = "";
input = input.toLowerCase();
String[] arr = input.split(" ");
for (String s : arr) {
if (!s.equals("") && !s.equals(null)) {
result += String.valueOf(s.charAt(0)).toUpperCase() + s.substring(1) + " ";
}
}

if (!result.equals("") && !result.equals(null)) {
result = result.substring(0, result.length() - 1);
}

return result;
}

public String chuanHoaTen1(String str){
String result = "";
str = str.toLowerCase();
StringTokenizer tokenizer = new StringTokenizer(str);
while (tokenizer.hasMoreElements()) {
String s = tokenizer.nextToken();
result += String.valueOf(s.charAt(0)).toUpperCase() + s.substring(1) + " ";
}

if(!result.equals("")){
result = result.substring(0, result.length() - 1);
}

return result;
}

public String chuanHoaTen2(String str){
String result = "";
str = str.toLowerCase();
StringTokenizer tokenizer = new StringTokenizer(str, " ");
while (tokenizer.hasMoreElements()) {
String s = tokenizer.nextToken();
result += String.valueOf(s.charAt(0)).toUpperCase() + s.substring(1) + " ";
}

if(!result.equals("")){
result = result.substring(0, result.length() - 1);
}

return result;
}

public String chuanHoaTen3(String str){
String result = "";
str = str.toLowerCase();
StringTokenizer tokenizer = new StringTokenizer(str, " ", true);
while (tokenizer.hasMoreElements()) {
String s = tokenizer.nextToken();
result += String.valueOf(s.charAt(0)).toUpperCase() + s.substring(1);
}

return result;
}

public static void main(String[] args) {
System.out.println(new ChuanChuoi().ChuanChuoi("nGuyEN vAn tiEN"));
System.out.println(new ChuanChuoi().chuanHoaTen1("nGuyEN vAn tiEN"));
System.out.println(new ChuanChuoi().chuanHoaTen2("nGuyEN vAn tiEN"));
System.out.println(new ChuanChuoi().chuanHoaTen3("nGuyEN vAn tiEN"));
}
}

Chúc các bạn học tốt!

About Ngo Thang IT

Check Also

Hướng dẫn tạo mã CSR và Private Key trong PHP

Để tạo được chứng chỉ SSL chúng ta cần có CSR và Private Key, trong …

Một bình luận

  1. I just want to tell you that I’m newbie to weblog and really liked this blog. Most likely I’m going to bookmark your site . You absolutely have incredible articles. Thanks a lot for revealing your web-site.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Đăng Ký Nhận Tin Qua Email

Nhận thông tin bổ ích về WordPress, Hosting, Thủ thuật website, Download miễn phí....và các phần quà hấp dẫn qua email.

* là bắt buộc