字符串编程

java字符串编程题

Java代码
  1. import java.util.HashMap;   
  2. import java.util.Map;   
  3.   
  4. public class CountCharNum {   
  5.   
  6. /**  
  7. * JAVA编程题:字符串"yekmaakkccekymbvb",求出字符串中有多少种字符,以及每个字符的个数?  
  8.  
  9. */  
  10. public static void main(String[] args) {   
  11.   
  12.     String aString = "yekmaakkccekymbvb";   
  13.     int count = 0;   
  14.     String singleString = "";   
  15.     Map stringMap = new HashMap();   
  16.     for (int i = 0; i < aString.length(); i++) {   
  17.         singleString = String.valueOf(aString.charAt(i));   
  18.         System.out.println(singleString);   
  19.         CountCharNum ccn = new CountCharNum();   
  20.         // 第一次出现的字符    
  21.         if (stringMap.get(singleString) == null) {   
  22.             count = 0;   
  23.         } else {   
  24.             count =  (Integer)stringMap.get(singleString);   
  25.         }   
  26.         stringMap.put(singleString, count + 1);   
  27.         }   
  28.         System.out.println(stringMap);   
  29.     }   
  30. }  
import java.util.HashMap;
import java.util.Map;

public class CountCharNum {

/**
* JAVA编程题:字符串"yekmaakkccekymbvb",求出字符串中有多少种字符,以及每个字符的个数?
* 
*/
public static void main(String[] args) {

	String aString = "yekmaakkccekymbvb";
	int count = 0;
	String singleString = "";
	Map stringMap = new HashMap();
	for (int i = 0; i < aString.length(); i++) {
		singleString = String.valueOf(aString.charAt(i));
		System.out.println(singleString);
		CountCharNum ccn = new CountCharNum();
		// 第一次出现的字符	
		if (stringMap.get(singleString) == null) {
		    count = 0;
		} else {
		    count =  (Integer)stringMap.get(singleString);
		}
		stringMap.put(singleString, count + 1);
		}
		System.out.println(stringMap);
	}
}


Java代码 复制代码
  1. public class StringSplit {   
  2.   
  3. /**  
  4. * 编程:编写一个截取字符串的函数, 输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如"我ABC"4,  
  5. * 应该截为"我AB",输入"我ABC汉DEF"6, 应该输出为"我ABC"而不是"我ABC+汉的半个"。  
  6.  
  7. * @param args  
  8. */  
  9. String splitString;   
  10. int splitBytes;   
  11.   
  12. public StringSplit(String splitString, int splitBytes) {   
  13.     super();   
  14.     this.splitString = splitString;   
  15.     this.splitBytes = splitBytes;   
  16.     System.out.println("The string is : " + splitString   
  17.         + ",the splitbytes is :" + splitBytes);   
  18.     }   
  19.   
  20. public void splitIt() {   
  21.     int num = 0;   
  22.     StringBuffer sb = new StringBuffer();   
  23.     int i = 0;   
  24.     if (splitBytes >= 2) {   
  25.          while (num < splitBytes) {   
  26.         if (splitString.substring(i, i + 1).matches("[/u4e00-/u9fa5]+")) {   
  27.             num += 2;   
  28.         } else {   
  29.             num++;   
  30.         }   
  31.         sb.append(splitString.substring(i, i + 1));   
  32.         i++;   
  33.         }   
  34.         System.out.println("final string :" + sb.toString());   
  35.     }   
  36. }   
  37.   
  38. public static void main(String[] args) {   
  39.     StringSplit ss = new StringSplit("我ABCD妈E爸F"5);   
  40.     ss.splitIt();   
  41.     }   
  42. }  
public class StringSplit {

/**
* 编程:编写一个截取字符串的函数, 输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如"我ABC"4,
* 应该截为"我AB",输入"我ABC汉DEF"6, 应该输出为"我ABC"而不是"我ABC+汉的半个"。
* 
* @param args
*/
String splitString;
int splitBytes;

public StringSplit(String splitString, int splitBytes) {
	super();
	this.splitString = splitString;
	this.splitBytes = splitBytes;
	System.out.println("The string is : " + splitString
		+ ",the splitbytes is :" + splitBytes);
	}

public void splitIt() {
	int num = 0;
	StringBuffer sb = new StringBuffer();
	int i = 0;
	if (splitBytes >= 2) {
	     while (num < splitBytes) {
		if (splitString.substring(i, i + 1).matches("[/u4e00-/u9fa5]+")) {
		    num += 2;
		} else {
		    num++;
		}
		sb.append(splitString.substring(i, i + 1));
		i++;
		}
		System.out.println("final string :" + sb.toString());
	}
}

public static void main(String[] args) {
	StringSplit ss = new StringSplit("我ABCD妈E爸F", 5);
	ss.splitIt();
    }
}

Java代码 复制代码
  1. package com.searchkiller;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.Collections;   
  5. import java.util.List;   
  6.   
  7. public class AlphaAsc {   
  8.   
  9.     /**Java编程题:字符串sbc,fds,des按字母的升序输出  
  10.      * @param args  
  11.      */  
  12.     public static void main(String[] args) {   
  13.   
  14.         String[] aString = {"sbc","fds","des"};   
  15.   
  16.         List list = new ArrayList();   
  17.         for (int i = 0; i < aString.length; i++) {   
  18.             list.add(aString[i]);   
  19.         }   
  20.         Collections.sort(list);   
  21.         System.out.println(list);   
  22.     }   
  23.   
  24. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值