Android 简单统计文本文件的字符数、单词数、行数、语句数Demo

一、图形界面部分代码:

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_writ"
        android:singleLine="false"
        android:hint="请输入数据"
        android:imeOptions="actionDone"      />// 监听回车键

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="开始解析数据"
        android:textSize="20dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tv_v"
            android:text="显示数据"/>

二、核心功能代码:

 private void Start() {
        int wd = 0;//单词数
        int by = 0;//字符数
        int sentence = 0;//句子数
        int lines = 0;//行数
        char num = 10;  //Ascii换行
        char nums = 13; //Ascii回车
        String s = et_writ.getText().toString().trim();//获取数据
            for (int i = 0; i < s.length(); i++) { //得到所输入的数据的长度,并遍历
                char c = s.charAt(i); 
                if (c == ' ') wd++; //单词
                if (c==num ||c==nums) ++lines;
                if (c == '.' || c == '!' || c == '?')
                    if (i > 0 && s.charAt(i - 1) != '.' && s.charAt(i - 1) != '?' && s.charAt(i - 1) != '!') {
                        sentence++;
                        wd++;
                    }
                by++;
            }
                ++lines;
        tv_v.setText("字符:"+by+"\n单词:"+wd+"\n句子:"+sentence+"\n行数:"+lines);
    }

    }
posted @ 2017-03-27 23:00  荒、年  阅读(1549)  评论(0编辑  收藏  举报