java将数字替换为空_将数字替换为java中正确位置的单词
實際上我正試圖用用戶給出的句子中的數字替換為單詞.本案例日期格式;例如:我的生日是在16/6/2000,我是
java的新手 – >成為—>我的生日是七月十六日,我是java的新手
這是代碼:
Scanner reader = new Scanner(System.in);
System.out.println("Enter any numbers: ");
String nom = reader.nextLine(); // get input from user
//checking contains that has "/" or not
if(nom.contains("/")){
String parts[] = nom.split("[/]");
String part1 = parts[0]; //data before "/" will be stored in the first array
String day[] = part1.split("\\s+");// split between space
String get_day = day[day.length -1];// get last array
String get_month = parts[1]; //data in the between of "/" will be stored in the second array
String part3 = parts[2]; // data after "/" will be stored in the third array
String year[] = part3.split("\\s+");// split between space
String get_year = year[0];// get first array
String s = NumberConvert.convert(Integer.parseInt(get_day)) +
NumberConvert.convert(Integer.parseInt(get_month)) +
NumberConvert.convert(Integer.parseInt(get_year));
String con = nom.replaceAll("[0-9].*/[0-9].*/[0-].*", s); // replace number to word
System.out.println(con); // print the data already converted
} else {....}
但我得到的結果是:
My birthday is on sixteenth july two thousand
//"and I'm newbie to the java" is disappear [How to solve it]//
如何解決它.實際上我想獲得“/”斜杠之前和之后的值并將其轉換為單詞并將其替換為來自用戶的原始輸入.
我試過的是:
String con = nom.replaceAll("[0-9].*/[0-9].*/[0-9999]", s); // a bit change [0-9].* to [0-9999]
但輸出變成這樣:
My birthday is on sixteenth july two thousand 000 and I'm newbie to the java
//after two thousand index "000" is appearing
總結
以上是生活随笔為你收集整理的java将数字替换为空_将数字替换为java中正确位置的单词的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 画股票图csdn_这个股票今天是要弄啥?
- 下一篇: 小米笔记本引导修复_知识帖,笔记本电脑不