用java截取字符串
请问下编写一个程序,要求如下:已知字符串“abcd123中华人民”
用截取功能,输入8的时候,输出"abcd123中(半个中字)"
输入9的时候输出"abcd123中(完整的中字)"
追问:输入8的时候,没有“中”字,输入9的时候,出现了整个“中字”,我希望看到的是,输入8的时候,输出半个“中”字
请问下编写一个程序,要求如下:已知字符串“abcd123中华人民”
用截取功能,输入8的时候,输出"abcd123中(半个中字)"
输入9的时候输出"abcd123中(完整的中字)"
追问:输入8的时候,没有“中”字,输入9的时候,出现了整个“中字”,我希望看到的是,输入8的时候,输出半个“中”字
答案:满足题意,功能扩展,请测试...import java.util.Scanner;
public class Test4 {
public static void main(String args[]) {
System.out.println ("java截取字符串实例(请输入一个字符串,按回车结束):\n");
String str = "",subStr = "";
int num = 0,start = 0,end = 0;
Scanner in=new Scanner(System.in);
System.out.print("原字符串:");
str = in.nextLine ();
num = str.length ();
System.out.println("您刚才总共输入了" + num+"个字符,请输入截取子字符串起止位置的下标[0~"+(num-1)+"]");
System.out.print("开始位置:");
in=new Scanner(System.in);
start = in.nextInt ();
if(start<0||start>num-1){
System.out.println("错误 :下标的范围必须在[0~"+(num-1)+"]内!");
}else{
System.out.print("结束位置:");
end = in.nextInt ();
if(end<0||start>num-1){
System.out.println("错误 :下标的范围必须在[0~"+(num-1)+"[内!");
}else{
subStr = str.substring(start, end);
System.out.println("子字符串:"+subStr);
}
}
}
}public class test{
public void splitIt(String splitStr, int bytes) {
int cutLength = 0;
int byteNum = bytes;
byte bt[] = splitStr.getBytes();
System.out.println("Length of this String ===>" + bt.length);
if (bytes > 1) {
for (int i = 0; i < byteNum; i++) {
if (bt[i] < 0) {
cutLength++;
}
}
if (cutLength % 2 == 0) {
cutLength /= 2;
}else
{
cutLength=0;
}
}
int result=cutLength+--byteNum;
if(result>bytes)
{
result=bytes;
}
if (bytes == 1) {
if (bt[0] < 0) {
result+=2;
}else
{
result+=1;
}
}
String substrx = new String(bt, 0, result);
System.out.println(substrx);
}
public static void main(String args[]) {
String str = "abcd123中华人民";
int num =3;
System.out.println("num:" + num);
test sptstr = new test();
sptstr.splitIt(str, num);
}}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class Test
{
public static void main(String args[])
{
String str = "abcd123中华人民";
System.out.println("请输入一个数字:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int number = 0;
try
{
number = Integer.parseInt(br.readLine());
} catch (NumberFormatException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(number == 8)
{
System.out.println(str.subSequence(0, number-1));
}
if(number == 9)
{
System.out.println(str.subSequence(0, number-1));
}
}
}public static String getString(String str,int leng){
return new String(str.getBytes(),0,leng);
}System.out.println(getString("abcd123中华人民",8));
System.out.println(getString("abcd123中华人民",9));
public class Test {
public String splitIt(String splitStr, int bytes) {
String sub = new String(splitStr.getBytes(),0,bytes);
byte[] cvalues = splitStr.substring (0, sub.length()).getBytes();
int clength = cvalues.length;
String end="";
if(clength>bytes){
bytes+=1;
end+="(半个?字)";
sub = new String(splitStr.getBytes(),0,bytes);
end = end.replace("?",sub.charAt(sub.length()-1)+"");
return sub+end;
}sub = new String(splitStr.getBytes(),0,bytes);
return sub;
}
public static void main(String args[]) {
String str = "abcd123中华人民";
int num = 10;
System.out.println("num:" + num);
Test sptstr = new Test();
System.out.println(sptstr.splitIt(str, num));
}}
晕 难道你就为了一个String类中的一个方法而跑来问问用高分提问么?
我是一名网络教师, 如果你想学习JAVA的话可以加
扣的 65463,5195
嗯 一天的课程是这样的 每天上课三小时 晚七点到晚十点吧
然后就是 远程桌面连接授课,语音交流等等
也就是所谓的手把手拉起一只菜鸟吧
不过是要学费的 一个小时三元。
觉得有点不妥的话可以试听课
上一个:北京比较好的java培训学校
下一个:java 小问题 创建对象