1. 程式人生 > >Android 獲取字串首字母,並對字串列表進行排序。

Android 獲取字串首字母,並對字串列表進行排序。

最近在瞭解環信的過程中,看到裡面有一個可以根據使用者名稱對使用者進行排序的處理。今天單獨測試了一下, 在此做一個整理。

由於工具類HanziToPinyin.class程式碼量有點多,我就放到最後。

先來看獲取一箇中文字串的首字母方法:

/**
   * 根據字串獲取當前首字母
   *
   * @param name
   * @return
   */
  static String getLetter(String name) {
      String DefaultLetter = "#";
      if (TextUtils.isEmpty(name)) {
          return
DefaultLetter; } char char0 = name.toLowerCase().charAt(0); if (Character.isDigit(char0)) { return DefaultLetter; } ArrayList<HanziToPinyin.Token> l = HanziToPinyin.getInstance().get(name.substring(0, 1)); if (l != null && l.size() > 0 && l.get(0
).target.length() > 0) { HanziToPinyin.Token token = l.get(0); // toLowerCase()返回小寫, toUpperCase()返回大寫 String letter = token.target.substring(0, 1).toLowerCase(); char c = letter.charAt(0); // 這裡的 'a' 和 'z' 要和letter的大小寫保持一直。 if (c < 'a' || c > 'z'
) { return DefaultLetter; } return letter; } return DefaultLetter; }

如果獲取失敗的話,就返回預設值“#”如果letter的大小寫,和 if 裡面所比較的大小寫不一致的話,也是會直接返回“#”。
在使用的時候,直接呼叫getLetter 傳入你需要獲取首字母的字串就可以了。

我這裡造了一些假資料用來測試:

public class HzPyDataModel {

    public static List<User> getList() {
        List<User> list = new ArrayList<User>();
//        劉一 陳二 張三 李四 王五 趙六 孫七 周八 吳九 鄭十
        User user = new User();
        user.setNick("劉一");
        user.setWholePy("liuyi");

        User user1 = new User();
        user1.setNick("陳二");
        user1.setWholePy("chener");

        User user2 = new User();
        user2.setNick("張三");
        user2.setWholePy("zhangsan");

        User user3 = new User();
        user3.setNick("李四");
        user3.setWholePy("lisi");

        User user4 = new User();
        user4.setNick("王五");
        user4.setWholePy("wangwu");

        User user5 = new User();
        user5.setNick("趙六");
        user5.setWholePy("zhaoliu");

        User user6 = new User();
        user6.setNick("孫七");
        user6.setWholePy("sunqi");

        User user7 = new User();
        user7.setNick("周八");
        user7.setWholePy("zhouba");

        User user8 = new User();
        user8.setNick("吳九");
        user8.setWholePy("wujiu");

        User user9 = new User();
        user9.setNick("鄭十");
        user9.setWholePy("zhengshi");

        list.add(user);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        list.add(user6);
        list.add(user7);
        list.add(user8);
        list.add(user9);
        return list;
    }

    public static class User {
        private String initial;//使用者首字母
        private String nick;// 使用者暱稱
        private String wholePy;// 使用者全拼

        public String getInitial() {
            return initial;
        }

        public void setInitial(String initial) {
            this.initial = initial;
        }

        public String getNick() {
            return nick;
        }

        public void setNick(String nick) {
            this.nick = nick;
        }

        public String getWholePy() {
            return wholePy;
        }

        public void setWholePy(String wholePy) {
            this.wholePy = wholePy;
        }
    }
}

測試方法為:

private List<User> contactList = new ArrayList<User>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    contactList = HzPyDataModel.getList();

    for (User user : contactList) {
        user.setInitial(getLetter(user.getNick()));
        Log.i(TAG, user.getNick() + ": " + user.getInitial());
    }
}

執行結果:

因為上面已經對每一個User都設定了其對應的首字母,然後我們來根據首字母進行排序:

public void sort() {
        // 排序
        Collections.sort(contactList, new Comparator<User>() {
            @Override
            public int compare(User lhs, User rhs) {
                if (lhs.getInitial().equals(rhs.getInitial())) {
                    return lhs.getNick().compareTo(rhs.getNick());
                } else {
                    if ("#".equals(lhs.getInitial())) {
                        return 1;
                    } else if ("#".equals(rhs.getInitial())) {
                        return -1;
                    }
                    return lhs.getInitial().compareTo(rhs.getInitial());
                }
            }
        });
    }

繼續在上面的for迴圈下面呼叫排序方法,

sort();
for (User user : contactList) {
      Log.i(TAG, user.getNick() + ": " + user.getWholePy());
}

執行結果:

用法就是這樣,最後是工具類:HanziToPinyin.class

public class HanziToPinyin {
    private static final String TAG = "HanziToPinyin";
    private static final boolean DEBUG = false;
    public static final char[] UNIHANS = new char[]{'阿', '哎', '安', '骯', '凹', '八', '挀', '扳', '邦', '勹', '陂', '奔', '伻', '屄', '邊', '灬', '憋', '汃', '冫', '癶', '峬', '嚓', '偲', '參', '倉', '撡', '冊', '嵾', '曽', '曾', '層', '叉', '芆', '辿', '倀', '抄', '車', '抻', '沈', '沉', '阷', '吃', '充', '抽', '出', '欻', '揣', '巛', '刅', '吹', '旾', '逴', '呲', '匆', '湊', '粗', '汆', '崔', '邨', '搓', '咑', '呆', '丹', '當', '刀', '嘚', '扥', '燈', '氐', '嗲', '甸', '刁', '爹', '丁', '丟', '東', '吺', '厾', '耑', '襨', '噸', '多', '妸', '誒', '奀', '鞥', '兒', '發', '帆', '匚', '飛', '分', '豐', '覅', '仏', '紑', '伕', '旮', '侅', '甘', '岡', '皋', '戈', '給', '根', '刯', '工', '勾', '估', '瓜', '乖', '關', '光', '歸', '丨', '咼', '哈', '咍', '佄', '夯', '茠', '訶', '黒', '拫', '亨', '噷', '叿', '齁', '乯', '花', '懷', '犿', '巟', '灰', '昏', '吙', '丌', '加', '戔', '江', '艽', '階', '巾', '坕', '冂', '丩', '凥', '姢', '噘', '軍', '咔', '開', '刊', '忼', '尻', '匼', '肎', '劥', '空', '摳', '扝', '誇', '蒯', '寬', '匡', '虧', '坤', '擴', '垃', '來', '蘭', '啷', '撈', '肋', '勒', '崚', '刕', '倆', '奩', '良', '撩', '列', '拎', '刢', '溜', '囖', '龍', '瞜', '嚕', '孌', '畧', '掄', '羅', '呣', '媽', '埋', '嫚', '牤', '貓', '麼', '呅', '門', '甿', '咪', '宀', '喵', '乜', '民', '名', '謬', '摸', '哞', '毪', '嗯', '拏', '腉', '囡', '囔', '孬', '疒', '娞', '恁', '能', '妮', '拈', '嬢', '鳥', '捏', '囜', '寧', '妞', '農', '羺', '奴', '奻', '瘧', '黁', '郍', '喔', '謳', '妑', '拍', '眅', '乓', '拋', '呸', '噴', '匉', '丕', '囨', '剽', '氕', '姘', '乒', '釙', '剖', '僕', '七', '掐', '千', '嗆', '悄', '癿', '親', '狅', '芎', '丘', '區', '峑', '缺', '夋', '呥', '穣', '嬈', '惹', '人', '扔', '日', '茸', '厹', '邚', '挼', '堧', '婑', '瞤', '捼', '仨', '毢', '三', '桒', '掻', '閪', '森', '僧', '殺', '篩', '山', '傷', '弰', '奢', '申', '莘', '敒', '升', '屍', '収', '書', '刷', '衰', '閂', '雙', '誰', '吮', '說', '厶', '忪', '捜', '蘇', '狻', '夊', '孫', '唆', '他', '囼', '坍', '湯', '夲', '忑', '熥', '剔', '天', '旫', '帖', '廳', '囲', '偷', '凸', '湍', '推', '吞', '乇', '穵', '歪', '彎', '尣', '危', '昷', '翁', '撾', '烏', '夕', '虲', '仚', '鄉', '灱', '些', '心', '星', '凶', '休', '籲', '吅', '削', '坃', '丫', '懨', '央', '么', '倻', '一', '囙', '應', '喲', '傭', '優', '扜', '囦', '曰', '暈', '筠', '篔', '帀', '災', '兂', '匨', '傮', '則', '賊', '怎', '増', '扎', '捚', '沾', '張', '長', '長', '佋', '蜇', '貞', '爭', '之', '峙', '庢', '中', '州', '朱', '抓', '拽', '專', '妝', '隹', '宒', '卓', '乲', '宗', '鄒', '租', '鑽', '厜', '尊', '昨', '兙', '鿃', '鿄'};
    public static final byte[][] PINYINS = new byte[][]{{(byte)65, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)65, (byte)73, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)65, (byte)78, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)65, (byte)78, (byte)71, (byte)0, (byte)0, (byte)0}, {(byte)65, (byte)79, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)65, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)65, (byte)73, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)65, (byte)78, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)65, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)66, (byte)65, (byte)79, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)69, (byte)73, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)69, (byte)78, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)69, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)66, (byte)73, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)73, (byte)65, (byte)78, (byte)0, (byte)0}, {(byte)66, (byte)73, (byte)65, (byte)79, (byte)0, (byte)0}, {(byte)66, (byte)73, (byte)69, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)73, (byte)78, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)73, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)66, (byte)79, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)66, (byte)85, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)65, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)65, (byte)73, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)65, (byte)78, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)65, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)67, (byte)65, (byte)79, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)69, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)69, (byte)78, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)69, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)90, (byte)69, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)67, (byte)69, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)65, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)65, (byte)73, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)65, (byte)78, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)65, (byte)78, (byte)71, (byte)0}, {(byte)67, (byte)72, (byte)65, (byte)79, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)69, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)69, (byte)78, (byte)0, (byte)0}, {(byte)83, (byte)72, (byte)69, (byte)78, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)69, (byte)78, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)69, (byte)78, (byte)71, (byte)0}, {(byte)67, (byte)72, (byte)73, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)79, (byte)78, (byte)71, (byte)0}, {(byte)67, (byte)72, (byte)79, (byte)85, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)85, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)85, (byte)65, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)85, (byte)65, (byte)73, (byte)0}, {(byte)67, (byte)72, (byte)85, (byte)65, (byte)78, (byte)0}, {(byte)67, (byte)72, (byte)85, (byte)65, (byte)78, (byte)71}, {(byte)67, (byte)72, (byte)85, (byte)73, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)85, (byte)78, (byte)0, (byte)0}, {(byte)67, (byte)72, (byte)85, (byte)79, (byte)0, (byte)0}, {(byte)67, (byte)73, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)79, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)67, (byte)79, (byte)85, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)85, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)85, (byte)65, (byte)78, (byte)0, (byte)0}, {(byte)67, (byte)85, (byte)73, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)85, (byte)78, (byte)0, (byte)0, (byte)0}, {(byte)67, (byte)85, (byte)79, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)65, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)65, (byte)73, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)65, (byte)78, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)65, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)68, (byte)65, (byte)79, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)69, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)69, (byte)78, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)69, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)68, (byte)73, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)73, (byte)65, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)73, (byte)65, (byte)78, (byte)0, (byte)0}, {(byte)68, (byte)73, (byte)65, (byte)79, (byte)0, (byte)0}, {(byte)68, (byte)73, (byte)69, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)73, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)68, (byte)73, (byte)85, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)79, (byte)78, (byte)71, (byte)0, (byte)0}, {(byte)68, (byte)79, (byte)85, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)85, (byte)0, (byte)0, (byte)0, (byte)0}, {(byte)68, (byte)85, (byte)65, (byte)78, (byte)0, (byte)0}, {(byte)68, (byte)85, (byte)73, (byte)0, (byte)
            
           

相關推薦

Android 獲取字串字母字串列表進行排序

最近在瞭解環信的過程中,看到裡面有一個可以根據使用者名稱對使用者進行排序的處理。今天單獨測試了一下, 在此做一個整理。 由於工具類HanziToPinyin.class程式碼量有點多,我就放到最後。 先來看獲取一箇中文字串的首字母方法: /** *

在servlet中獲取jsp頁面中選中的複選框的值其值進行操作

jsp部分程式碼:<input type="checkbox" name="checkboxs" value="${stuInfo.sid}"/>動態的為複選框賦值servlet程式碼:String[] ids = request.getParameterValu

JS獲取中文拼音字母通過拼音字母快速查詢頁面內的中文內容

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

演算法 -- 四種方法獲取的最長“迴文串”時間複雜進行分析對比&PHP

迴文串: “迴文串”是一個正讀和反讀都一樣的字串,比如“level”或者“noon”等等就是迴文串。 -- 來自百度百科 關於獲取字串中最長的迴文串的演算法中,目前有很多演算法,本文中主要是用PHP來實現的演算法之一。 演算法一:暴力解法 暴力計算出所有的字串並判斷。時間複雜

獲取當前正在執行的虛擬機器(Vmware Workstation)虛擬機器進行控制

vmware workstation 提供了一個命令列工具進行虛擬機器管理 C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe

去除String中的html標籤進行替換

import java.util.regex.Matcher; import java.util.regex.Pattern; private String cleanHtmlTags(String htmlText) { if (StringUti

讀取Excel檔案Excel檔案進行描述性分析

> mydata<-read.csv("E:\\╰凝初寒つ\\學習檔案\\大資料概論\\R語言\\soapdata.csv",header=T) > dim(mydata) > mydata<-head(mydata,41) >

python 檔案和目錄 當前目錄以及當前目錄的所有子目錄下查詢檔名包含指定字串的檔案打印出相對路徑

廖雪峰python教程中的<操作檔案和目錄>下的第二題目的解答, 網址為: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319253

C#WPF中使用多文字顯示資料其資料進行關鍵字高亮等操作

需求:針對多文字資訊顯示,我們需要對其內容中的某些關鍵字或者某行進行高亮顯示,並用不同顏色顯示。 分析:在C#中,首先要進行多文字資訊顯示,可以RichTextBox(不要使用TextBox)控制元件,該控制元件由自動換行等功能,具體程式碼如下: /// <summary> /// 向

呼叫簡訊介面傳送驗證碼驗證碼進行驗證

首先需要對簡訊介面內容進行封裝:  public class SendMsg2Util { private static String url="http://gw.api.taobao.com/router/rest"; private static Str

Java 往TreeSet集合中儲存自定義物件學生按照學生的年齡進行排序

Set:無序,不可以重複元素。|--HashSet:資料結構是雜湊表。執行緒是非同步的。保證元素唯一性的原理:判斷元素的hashCode值是否相同。如果相同,還會繼續判斷元素的equals方法,是否為true。|--TreeSet:可以對Set集合中的元素進行排序。底層資料

【java】java反射機制動態獲取象的屬性和對應的參數值屬性按照字典序排序Field.setAccessible()方法的說明【可用於微信支付 簽名生成】

modifier 直接 this 字段值 1-1 讓我 toupper ima play 方法1:通過get()方法獲取屬性值 package com.sxd.test.controller; public class FirstCa{ private

輸入字串其大小寫和字元進行統計輸出

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //輸入字串,並對其大小寫和字元進行統計輸出 //經分析,除了英文,其他都是字元。 void main(void) { char

字串字母變更為大寫字母其餘小寫字母(兩種方法 substring()與charAt())

package demo_取部分字串將其轉換大小寫; public class toUpperCaseAndtoLowerCase { public static void main(String[] args) { toConvert("sdfdsgre")

建立一個長度是5的隨機字串隨機字元有可能是數字大寫字母或者小寫字母將它從小到大排序

無力吐槽。。。 基礎是真的差,一個直接排序搞了一下午都沒成功。僅僅是對於基本知識點理解不透徹,少加一個等號。 看別人找工作,語言是基礎,還要懂linux,資料結構與演算法。。。。。 加油吧。 public class suijizifuchuan { /**

字串轉換將其中的大寫字母變小寫字母小寫字母變大寫字母輸出

從鍵盤輸入一個字串,將其中的大寫字母變小寫字母,小寫字母變大寫字母,並輸出 。 【要求】   (1)用字元陣列表示儲存字串(字串最大為100)。   (2)使用scanf函式逐個輸入字元儲存在字元陣列

將圖片檔案轉化為位元組陣列字串進行Base64編碼處理和 位元組陣列字串進行Base64解碼生成圖片

public static String imageToBase64(String path) { // 將圖片檔案轉化為位元組陣列字串,並對其進行Base64編碼處理 byte[] data = null; // 讀取圖片位元組

輸入6個字串它們按從小到大的順序排序後輸出

printf("請輸入六個字串\n"); char str[6][10] = {0}; for (int i = 0; i < 6; i++) { scanf("%s",str[i]); } for (int i

根據城市字母進行分類區分多音字,獲取城市字母

多音字詞典  duoyinzi_pinyin.txt a#阿 ao#拗 ai#艾 bang#膀 磅 蚌 ba#扒車 扒拉 扒皮 bai#叔伯 百 柏楊 bao#剝皮 薄  暴 堡 曝 beng#蚌埠 bi#復辟  臂  祕魯 泌陽 bing#屏棄 屏氣 屏除 屏退 屏息 b