字符串编程
Posted 六楼外的风景
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串编程相关的知识,希望对你有一定的参考价值。
java字符串编程题
Java代码- 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);
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代码
- 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();
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代码
- package com.searchkiller;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class AlphaAsc
- /**Java编程题:字符串sbc,fds,des按字母的升序输出
- * @param args
- */
- public static void main(String[] args)
- String[] aString = "sbc","fds","des";
- List list = new ArrayList();
- for (int i = 0; i < aString.length; i++)
- list.add(aString[i]);
- Collections.sort(list);
- System.out.println(list);
以上是关于字符串编程的主要内容,如果未能解决你的问题,请参考以下文章