Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidで文字列内の各文字の出現をカウントする方法は?


この例は、Androidで文字列内の各文字の出現をカウントする方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/name"
      android:layout_width = "match_parent"
      android:hint = "Enter Name"
      android:layout_height = "wrap_content" />
   <LinearLayout
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content">
      <Button
         android:id = "@+id/save"
         android:text = "Save"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
   </LinearLayout>
   <TextView
      android:id = "@+id/textview"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent" />
</LinearLayout>

上記のコードでは、ユーザーがボタンをクリックすると名前が付けられ、文字列内の文字の出現をチェックし、結果をテキストビューで表示します。

ステップ3 −次のコードをsrc / MainActivity.java

に追加します
package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
   EditText name;
   HashMap<Character,Integer> charCountMap;
   TextView textview;
   @Override
   protected void onCreate(Bundle readdInstanceState) {
      super.onCreate(readdInstanceState);
      setContentView(R.layout.activity_main);
      name = findViewById(R.id.name);
      textview = findViewById(R.id.textview);
      charCountMap = new HashMap<>();
      findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (!name.getText().toString().isEmpty()) {
               char[] strArray = name.getText().toString().toCharArray();
               for(char charItem:strArray) {
                  if(charCountMap.containsKey(charItem)) {
                     charCountMap.put(charItem,charCountMap.get(charItem)+1);
                  } else {
                     charCountMap.put(charItem,1);
                  }
               }
               textview.setText(charCountMap.toString());
               Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
            } else {
               name.setError("Enter NAME");
            }
         }
      });
   }
}
アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–

Androidで文字列内の各文字の出現をカウントする方法は?

上記の結果では、テキストビューでの文字の出現数を取得しています。


  1. Androidアプリで通知の数を表示するにはどうすればよいですか?

    この例は、Androidアプリランチャーで通知の数を表示する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://

  2. 文字列内の文字の出現回数を Excel でカウント

    このチュートリアルでは、Excel の特定の文字列および文字列範囲内の文字または単語の出現回数について説明します。多くの場合、大量のデータを含むセルまたはデータ範囲内の文字の頻度を計算する必要があります。したがって、このカウント数を簡単にするために、この記事では使いやすい数式について説明します。 練習用ワークブックをダウンロード この記事の準備に使用した練習用ワークブックをダウンロードできます。 Excel で文字列内の文字の出現回数を数える 5 つの方法 1. SUMPRODUCT と LEN 関数を使用して、Excel で文字列内の文字出現回数の合計を求める セル内の総文字数を知