zoukankan      html  css  js  c++  java
  • 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+"
    单词:"+wd+"
    句子:"+sentence+"
    行数:"+lines);
        }
    
        }
    
  • 相关阅读:
    完整的WSDL语法
    WSDL UDDI
    八一八 The Social Network的小细节
    MySQL命令行常用命令
    AspectJ风格的Aop切点表达式
    强大的Mockito测试框架
    MySQL锁定状态查看命令
    Yum本地Rpm库设置
    Sed实例大全
    为何 Emacs 和 Vim 被称为两大神器
  • 原文地址:https://www.cnblogs.com/qq1107625225/p/6629322.html
Copyright © 2011-2022 走看看