第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用java substring函數(shù)提取出5個單詞?該怎么操作比較好?

如何使用java substring函數(shù)提取出5個單詞?該怎么操作比較好?

鳳凰求蠱 2021-10-11 14:10:11
有一個字符串 string str="I like apple and pear";我想用substring函數(shù)分別提取出這5個單詞,請問如何實(shí)現(xiàn)?請舉例說明,謝謝謝謝回復(fù)!如果字符串里的文字是隨機(jī)的要如何解決呢?
查看完整描述

2 回答

?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗 獲得超7個贊

可以將字符串按照空格分隔。。
string temp[]=str.split(" ");這個temp數(shù)組里面就是所有的單詞了。。也可以解決
文字隨機(jī)情況



查看完整回答
反對 回復(fù) 2021-10-16
?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗 獲得超9個贊

public class Test {    public static void main(String[] args) {        String str = "i like apple and pear";        String temp = str.substring(01);        String temp1 = str.substring(26);        String temp2 = str.substring(712);        String temp3 = str.substring(1316);        String temp4 = str.substring(1721);        System.out.println(temp + "," + temp1 + "," + temp2 + "," + temp3 + ","                + temp4);    }}

 如果文字是隨機(jī)的,可以通過String 的split()方法,以空格為分隔符,將文字變成字符數(shù)組,再輸出:


public class Test {    public static void main(String[] args) {            String str = "a b a df d a f da fa d ";            String[] arr = str.split(" "); //返回數(shù)組arr            for(String s:arr)                System.out.println(s);        }}

 如果一定要求要用substring()來實(shí)現(xiàn),必須用indexOf()返回空格的位置,再用substring返回具體字符:


public class Test {    public static void main(String[] args) {        String str = "i like  apple and pear";        find(str);    }     public static void find(String s) {        int i = s.indexOf(" ");        String temp;        if (i != -1) {            temp = s.substring(0, i);            System.out.println(temp);            if (s.length() > 0) {                String new_s = s.substring(i).trim();                find(new_s);            }        }else{            System.out.println(s);        }    }}

 已測試,如下圖:

 


查看完整回答
反對 回復(fù) 2021-10-16
  • 2 回答
  • 0 關(guān)注
  • 251 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號