用 Java 將字串標記化


我們有下列字串 -

String str = "This is demo text, and demo line!";

若要標記化字串,讓我們在每個句號 (.) 和逗號 (,) 後對其進行拆分

String str = "This is demo text, and demo line!";

下面是完整的示例。

示例

 線上演示

public class Demo {
    public static void main(String[] args) {
       String str = "This is demo text, and demo line!";
       String[] res = str.split("[, .]", 0);
       for(String myStr: res) {
          System.out.println(myStr);
       }
    }
}

輸出

This
is
demo
text

and
demo
line!

更新於: 26-06-2020

291 次瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.