设计函数,输入为一个字符串,里边包含中文、英文、数字等字符,编码为GBK。中文字符的编码规则假定为:双字节组成,高字节大于0x80,低字节任意。

2、设计函数,输入为一个字符串,里边包含中文、英文、数字等字符,编码为GBK。中文字符的编码规则假定为:双字节组成,高字节大于0x80,低字节任意。

    a) 用常用语言(c/c++/php/java)编写函数,实现功能为:按顺序提取输入文本中的中文字符,形成新的文本串返回调用者。

    b) 如果调用者有时希望得到输入串的全中文内容,有时希望得到英文内容,那么该函数应如何设计。

    c) 如果调用者希望获取输入串中包含中文、英文、数字这三种字符中的一种或多种的需求不定时,函数应如何设计。

 

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.util.Scanner;

 

/*

 * 创建日期 2010-10-1

 *

 * TODO 要更改此生成的文件的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

 

/**

 * @author zhchyun

 *

 * TODO 要更改此生成的类型注释的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

public class subString {

   

     //截取中文字符

    public static String getChinese(String str)throws UnsupportedEncodingException{

            String subChinese="";

            for(int i=0;i<str.length() ;i++){

                char ch=str.charAt( i);

                byte [] buf =(ch+"").getBytes("GBK");

                if (buf.length >1){

                    subChinese+=ch;

                   

                }

            }

                           

       

        return subChinese;

       

       

    }

   

    //截取英文字符

    public static String getEnglish(String str) throws UnsupportedEncodingException{

       

        String subEnglish="";

        for (int i=0;i<str.length() ;i++){

            char ch=str.charAt( i);

            byte[] buf=(ch+"").getBytes("GBK");

            if (buf.length ==1 && Character.isLetter( ch)){

                subEnglish+=ch;

            }

        }

        return subEnglish;

       

    }

    //截取数字

    public static String getDigit(String str) throws UnsupportedEncodingException{

        String subDigit="";

        for(int i=0;i<str.length() ;i++){

            char ch=str.charAt( i);

            byte[] buf=(ch+"").getBytes("GBK");

            if (buf.length ==1 && Character.isDigit(ch)){

                subDigit+=ch;

               

            }

        }

        return subDigit;

       

    }

   

 

    public static void main(String[] args) throws UnsupportedEncodingException {

       

        final int CHINESE=0x1;

        final int ENGLISH=0x2;

        final int DIGIT=0x3;

        int type;

  

        String str = null;

        String substr=null;

        try {

            str = new BufferedReader(new InputStreamReader(System.in)).readLine();

        } catch (IOException e) {

            // TODO 自动生成 catch

            e.printStackTrace();

        }

        System.out.println("源字符串为:"+str);

        System.out.println("请输入要截取字符串的类型:1.中文 2.英文 3.数字");

        Scanner sc=new Scanner(System.in);

        type=(int)sc.nextInt();

        if (type==1){

            substr=getChinese(str);

            System.out.println(" 截取中文字符为:");

            System.out.println(substr);

        }

        else if (type==2){

            System.out.println("截取英文字符为:");

            substr=getEnglish(str);

            System.out.print(substr);

        }

        else if (type==3){

            System.out.println("截取数字为:");

            substr=getDigit(str);

            System.out.print(substr);

        }

     

      

       

       

    }

}

这只是对第三问的解答,第一问可以只调用getChinese();第二问可以参照第三问,type只设置1和2 即可。个人答案仅供参考。只是还没有明白buf=(ch+"").getBytes("GBK");中为什么要ch+""才可以,是不是得到高位的GBK编号?

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhchyun2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值