[Java]Standardized strings in Java – Standardize string in java
To normalize the string in Java, we need to remove the extra white space at the top, end and mid-strings.
First we manually delete the spaces at the beginning and end by methods trim().
For example we have:
String str = " nguyen van quan 7826 "; str = str.trim();
Then
str = "nguyen van quan 7826"
Following our work will be cut off the extra white space in between the strings. To do this there are many ways, I outlined here 2 basically:
How to 1:
while (str.indexOf(" ") != -1) str = str.replaceAll(" "," ");
+/ s1.indexOf(s2) : method returns the position of s2 in s1.
+/ s.replaceAll(s1, s2) : method replaces all strings s1 into s2 in s. but just browse string s 1 time. So we need to combine all the while loop to be able to replace all.
How to 2:
str = str.replaceAll("\\s+", " ");
The illustrations
class java_chuanhoaxau { public static void main(String[] sgr) { String str = " nguyen van quan 7826 "; str = str.trim(); str = str.replaceAll("\\s+"," "); System.out.println(str); } }
=== update ===
As we have standardized on the chain by removing whitespace nonsense. Now we will normalize the words that will capitalize the first word and proper nouns like (Hanoi, Vietnam Nguyen Van Quan or, …)
To do this, we first normalized basis as above normal. The next step is capitalized the first letter of each word. I use the split method() to split the string into an array of words. Then use String.valueOf().toUperCase() to capitalize the first letter of each word in the end is connected to the rest of their characters in the substring(1).
So has the array from uppercase, followed by linking the words together and apart 1 spaces.
package vietSource.net; public class ChuanHoaXau { public String chuanHoa(String str) { str = str.trim(); str = str.replaceAll("\\s+", " "); return str; } public String chuanHoaDanhTuRieng(String str) { str = chuanHoa(str); String temp[] = str.split(" "); str = ""; // ? ^-^ for (int i = 0; i < temp.length; i++) { str += String.valueOf(temp[i].charAt(0)).toUpperCase() + temp[i].substring(1); if (i < temp.length - 1) // ? ^-^ str += " "; } return str; } public static void main(String[] sgr) { String str = " nguyen van quan 7826 "; ChuanHoaXau chx = new ChuanHoaXau(); str = chx.chuanHoaDanhTuRieng(str); System.out.println(str); } }
😀 hôm nay cô giáo dạy hướng đối tượng bọn em dùng bài code này của anh để hướng dẫn :v vẫn để nguyên là nguyễn văn quân 7826 :in :in
good…
Nếu ở giữa có nhiều hơn 1 dấu cách thì hàm split này có vẻ k ổn ạ!
Trước khi split thì đã chuẩn hóa để xóa hết các dấu cách thừa rồi nhé.
cho mình hỏi mấy cái ký tự \\s+ có ý nghĩa như thế nào? xin cảm ơn
Nó là ký hiệu regular, a set of symbols representing certain rules. Here is a white character.
I asked for the main method can segment the star SGR sir, because I usually see is args.. I thought it was mold. New Kids learn Java was two weeks sir!
It's just a variable name only.
Thanks for your article, Kids are headache because k knows remove whitespace between ntn
anh oi the vd ten: ” tr88a6n min123h q212uy ”
out put “Tran Minh Quy” lam ntn a?
This depends on the rules and percussion to treatment. Untreated like his never.
let me ask what the line if meaningful ạh
if the conditions to be something.
He let me ask paragraph “!=-1” in line “while (str.indexOf(” “) != -1) str = str.replaceAll(” “,” “);” This means that what was?
str.indexOf(” “) -> Locate spaces in str, if != -1 means. = -1 is no.
cái s=” ” ; for what?