Algs4-1.2(非习题)String

1.2String类型基本方法与属性

图片

图片

 public class Test
{
   public static void main(String[] args)
   {
     String s="000111222333";
     
     StdOut.printf("s is:%s\n",s);
     //
     StdOut.printf("s length=%d\n",s.length());
     //
     for(int i=0;i<s.length();i++)
         StdOut.printf("postion is %d,char is:%s\n",i,s.charAt(i));
     //
     for(int i=0;i<s.length();i++)
         StdOut.printf("char %s first postion is %d\n",s.charAt(i),s.indexOf(s.charAt(i)));
      //
     for(int i=0;i<s.length();i++)
         StdOut.printf("char %s 1st and 2nd postion is %d %d\n",s.charAt(i),s.indexOf(s.charAt(i)),s.indexOf(s.charAt(i),s.indexOf(s.charAt(i))+1));
     //
     String s2="";
     for(int i=0;i<s.length();i=i+3)
     {
         s2=s2.concat(s.substring(i,i+1)+ " ");
         StdOut.printf("s2 concat partion of s is:%s\n",s2);
     }
     //
     String[] words=s2.split(" ");
     for(int i=0;i<words.length;i++)
         StdOut.printf("words i %d is %s\n",i,words[i]);
     //
     StdOut.printf("s2 compare s2 is %s,s2 compare s is %s\n",s2.compareTo(s2),s2.compareTo(s));
     //
     StdOut.printf("s2 equals s2 is %s,s2 equals s is %s\n",s2.equals(s2),s2.equals(s));
   }
}

posted @ 2018-10-25 11:53  修电脑的龙生  阅读(154)  评论(0编辑  收藏  举报