字符串操作(1)

    技术2022-05-11  65

    /**********************************************Author:Java619*Time:2007-02-14**********************************************/

     今天群里有内提出,怎么从"djf${aaa}wfewf${bbb}dfe${ccc}dfef"分离出aaa,bbb,ccc,这边我给出个简单实现,大家有什么更好的办法可以发表下,谢谢!要实现更通用有方法可以使用正则表达式(祥看字符串操作(2)).

    import  java.util.ArrayList; public   class  StringToken  {    public static void main(String[] args)     {        String str="djf${aaa}wfewf${bbb}dfe${ccc}dfef";        int start=str.indexOf("$");        int last=str.lastIndexOf("$");        ArrayList<String> result=new ArrayList<String>();        for(int i=start;i<=last&&i>0;i=str.indexOf("$",i+5))           result.add(str.substring(i+2,i+5));                  for(String s:result)            System.out.printf("%s%n",s);    }}

    最新回复(0)