黑马程序员技术交流社区

标题: 字符串练习 [打印本页]

作者: zhoubinjian    时间: 2016-4-6 17:20
标题: 字符串练习
/*

去除字符串两端的空格
*/
class Demo
{
        public static void main(String[] args)
        {
                       
                String s="  asdfgh   ";
                sop("<"+s+">");
                sop("<"+sun(s)+">");

        }       
        public static String sun(String str)
        {
               
                int start=0,end=str.length()-1;
                while(start<=end && str.charAt(start)==' ')
                        start++;
                while(start<=end && str.charAt(end)==' ')
                        end--;
                return str.substring(start,end+1);//包头不包尾,所以要加1
        }
        public static void sop(Object e)
        {
                System.out.println(e);
        }
       
}
/*

将一个字符串进行反转,将字符串中指定部分进行反转。
*/

class Demo
{
        public static void main(String[] args)
        {
               
                       
                String s="--ab-cd--";
                sop("<"+s+">");
                sop("<"+sun(s,3,9)+">");
               

        }       

        public static String sun(String str,int st,int en)
        {
               
                char[] arr=str.toCharArray();
                res(arr,st,en);
                return new String(arr);
        }
        public static void res(char[] chs,int c,int d)
        {
                for(int start=c,end=d-1;start<end;start++,end--)
                {
                        swap(chs,start,end);
                }
        }

        public static void swap(char[] a,int x,int y)
        {
               
                char temp=a[x];
                        a[x]=a[y];
                        a[y]=temp;
               
        }

        public static void sop(Object e)
        {
                System.out.println(e);
        }
       
}
/*

获取一个字符串在另一个字符串中出现的次数
*/

class Demo
{
        public static void main(String[] args)
        {                       
                String s="asddefrddhrdd";
                sop("<"+s+">");
                sop("count="+sun(s,"dd"));
               

        }       

        public static int sun(String str,String key)
        {
                int count=0;
                int index=0;
                while((index=str.indexOf(key))!=-1)
                {
                        sop(str);
                        str=str.substring(index+key.length());
                        count++;
                }
               
                return count;
        }
       

        public static void sop(Object e)
        {
                System.out.println(e);
        }
       
}
/*
获取两个字符串中的最大子串

*/

class Demo
{
        public static void main(String[] args)
        {
               
                       
                String s1="ahyf";
                String s2="asrddeoiuh";
               
                sop(sun(s1,s2));
        }       
        public static String sun(String s1,String s2)
        {
                String max="",min="";
                max=(s1.length()>s2.length()?s1:s2);
                min=(max==s1?s2:s1);
                for(int x=0;x<min.length();x++)
                {
                        for(int y=0,z=min.length()-x;z!=min.length()+1;y++,z++)
                        {
                                String temp = min.substring(y,z);
                                if(max.contains(temp))
                                        return temp;
                        }
                       
                }
                return "";
               
        }

        public static void sop(Object e)
        {
                System.out.println(e);
        }
       
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2