关于String的简单编程题

一、将字符串反转

package string;

public class Reverse {

    public static void main(String[] args) {
        String s1 = "abc   defg wf";

        //用字符串转字符数组实现反转
        String s2 = "";
        char[] cs = s1.toCharArray();
        for (int i = cs.length - 1; i >= 0; i--) {
            s2 = s2 + cs[i];
        }
        System.out.println("方法一反转操作后为:" + s2);
        //利用StringBuffer的reverse()方法实现反转
        StringBuffer sb = new StringBuffer(s1);
        StringBuffer sb2 = sb.reverse();
        System.out.println("方法二反转操作后为:" + sb2);
        //利用倒序存储实现反转
        String s3=myReverse(s1);
        System.out.println("方法三反转操作后为:" + s3);


    }
    public static String myReverse(String str)  
    {  
        char[] arr = new char[str.length()];  
        int pos = str.length();  
        for (int x=0; x<str.length(); x++)  
        {  
            arr[x] = str.charAt(--pos);  
        }  
        str = String.copyValueOf(arr);  
        return str;  
    }  
}

输出:

方法一反转操作后为:fw gfed   cba
方法二反转操作后为:fw gfed   cba
方法三反转操作后为:fw gfed   cba

二、实现去字符串两端空格功能

package string;

public class Trime {
    public static void main(String[] args) {
        String str1 = "   dwwg dwg  wfg   ";
        //利用String类自己的方法去除两端空格
        String str2 = str1.trim();
        System.err.println("方法一"+str2);
        //自定义方法去除两端空格
        String str3=myTrim(str1);
        System.err.println("方法二"+str3);

    }

    public static String myTrim(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);
    }
}

输出:

方法一dwwg dwg  wfg
方法二dwwg dwg  wfg

三、获取一个字符串在另一个字符串中出现的次数,并返回出现起始索引

package string;

public class GetCount {

    public static void main(String[] args) {
        String str = "sshelloworldhellojavahellojsp";
        System.out.println("count=" + getSubCount(str, "hello"));
    }


    public static int getSubCount(String str, String key) {
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(key, index)) != -1) {
            System.out.println("index=" + index);
            index = index + key.length();
            count++;
        }
        return count;
    }

}


index=2
index=12
index=21
count=3

indexOf返回的是长字符串中所求子字符串起始处索引值,没有则返回-1.

四、将一段英文句子倒序输出

package string;
import java.util.Scanner;

public class Reverse02 {
    public static String reverseWords(String sentence) {

        StringBuilder sb = new StringBuilder(sentence.length() + 1);
        String[] words = sentence.split(" ");
        for (int i = words.length - 1; i >= 0; i--) {
            sb.append(words[i]).append(' ');
        }
        sb.setLength(sb.length() - 1);
        return sb.toString();
    }

    public static void main(String[] args)

    {
        Scanner in = new Scanner(System.in);
        // 存放输入的字符串
        System.out.println("输入测试用例数目n:");
        String[] input = new String[in.nextInt()];
        in.nextLine();

        for (int i = 0; i < input.length; i++) {
            input[i] = in.nextLine();
        }
        // 输出倒序后的英文字符串
        System.out.println("输出为:");
        for (String s : input) {
            System.out.println(reverseWords(s));
        }
    }
}
输入测试用例数目n:
2
hello world!
i love you?
输出为:
world! hello
you? love i

四、删除一个字符串中出现最多的字符

package string;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DelMost {
    public static void main(String[] args) {
        String str = "aassdfscfw";
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        char[] box = str.toCharArray();
        for (char ch : box) {
            Integer i = map.get(ch);
            if (i == null) {
                map.put(ch, 1);
            } else {
                map.put(ch, i + 1);
            }
        } // 创建map键值对

        Integer max = 0;
        Set<Character> set = map.keySet();
        for (Character key : set) {
            Integer j = map.get(key);
            if (max < j) {
                max = j;
            }
        } // 将最大次数传给max

        for (Character key : set) {
            if (map.get(key) == max) {
                str = str.replaceAll(key.toString(), "");
            }
        }
        System.out.println(str);

    }
}
aadfcfw

五、输入一行字符分别统计英文字母、数字、空格个数。

package string;

import java.util.Scanner;

public class Tongji {
    public static void main(String[] args) {
        int eng=0,num=0,kong=0,elsel=0;
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        char[] ch=null;
        ch=str.toCharArray();
        for(char c:ch){
            if(c>='0'&&c<='9'){
                num++;
            }else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
                eng++;
            }else if(c==' '){
                kong++;
            }else{
            elsel++;
            }
        }
        System.out.println("字母有"+eng);
        System.out.println("数字有"+num);
        System.out.println("空格有"+kong);
        System.out.println("其他"+elsel);
    }
}
hello world 378nihao
字母有15
数字有3
空格有2
其他0





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值