中文字符串拼音转化和首字母获取demo

构建springboot项目,pom文件如下:


    
    4.0.0
        
              org.springframework.boot
              spring-boot-starter-parent
              2.1.4.RELEASE
          
        
        com.cyclone
        initials
        0.0.1-SNAPSHOT
        initials

        
            1.8
            2.11.7
            2.11
        

  
    
        org.springframework.boot
        spring-boot-starter
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
        org.scala-lang
        scala-library
        ${scala.version}
    

    
        com.belerweb
        pinyin4j
        2.5.1
    



    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
        
            net.alchim31.maven
            scala-maven-plugin
            3.2.0
            
            
        
    


代码实现:

package com.cyclone
import net.sourceforge.pinyin4j.PinyinHelper
import net.sourceforge.pinyin4j.format.{HanyuPinyinCaseType, HanyuPinyinOutputFormat, HanyuPinyinToneType,    HanyuPinyinVCharType}

object Initials {
/**
* 将中文字符串分解为字,获取每个字拼音的首字母,组成字符串返回
*
* @param chineseString           需要进行转化的中文字符串,不能为null并且不能只包含非中文字符
* @param stripNoChineseCharacter 是否需要移除中文字符串中的非汉字字符,只有汉字字符才会进行转化,其他的非汉字字符原样返回
* @param caseType                返回字符串的大小写样式,默认为小写
* @param toneType                返回的字母的声调处理,默认为不带声调
* @param vCharType               返回字母的u处理方式,默认按v返回
* @return
*/
def chineseString2Initials(chineseString: String, stripNoChineseCharacter: Boolean = true, caseType: HanyuPinyinCaseType = HanyuPinyinCaseType.LOWERCASE,
                         toneType: HanyuPinyinToneType = HanyuPinyinToneType.WITHOUT_TONE,
                         vCharType: HanyuPinyinVCharType = HanyuPinyinVCharType.WITH_V): String = {
val tmp = if(stripNoChineseCharacter) strip(chineseString) else chineseString
val format = getFormatInstance(caseType, toneType, vCharType)
val result = StringBuilder.newBuilder
val words = tmp.toCharArray
words.foreach { (x: Char) =>
  if (x > 128) result.append(PinyinHelper.toHanyuPinyinStringArray(x, format)(0).charAt(0)) else result.append(x)
}
result.toString()
}


/**
* 将中文字符串分解为字,转化为拼音,组成字符串返回
*
* @param chineseString           需要进行转化的中文字符串,不能为null并且不能只包含非中文字符
* @param stripNoChineseCharacter 是否需要移除中文字符串中的非汉字字符,只有汉字字符才会进行转化,其他的非汉字字符原样返回
* @param caseType                返回字符串的大小写样式,默认为小写
* @param toneType                返回的字母的声调处理,默认为不带声调
* @param vCharType               返回字母的u处理方式,默认按v返回
* @return
*/
def chineseString2Letters(chineseString: String, stripNoChineseCharacter: Boolean = true, caseType: HanyuPinyinCaseType = HanyuPinyinCaseType.LOWERCASE,
                        toneType: HanyuPinyinToneType = HanyuPinyinToneType.WITHOUT_TONE,
                        vCharType: HanyuPinyinVCharType = HanyuPinyinVCharType.WITH_V): String = {
val tmp = if(stripNoChineseCharacter) strip(chineseString) else chineseString
val format = getFormatInstance(caseType, toneType, vCharType)
val result = StringBuilder.newBuilder
val words = tmp.toCharArray
words.foreach { (x: Char) =>
  if (x > 128) result.append(PinyinHelper.toHanyuPinyinStringArray(x, format)(0)) else result.append(x)
}
result.toString()
}

private def getFormatInstance(caseType: HanyuPinyinCaseType = HanyuPinyinCaseType.LOWERCASE,
                            toneType: HanyuPinyinToneType = HanyuPinyinToneType.WITHOUT_TONE,
                            vCharType: HanyuPinyinVCharType = HanyuPinyinVCharType.WITH_V): HanyuPinyinOutputFormat = {
val format = new HanyuPinyinOutputFormat
format.setCaseType(caseType)
format.setToneType(toneType)
format.setVCharType(vCharType)
format
}

//移除字符串中的非中文字符
private def strip(arg: String): String = {
if(arg ==null) throw  new NullPointerException
val regex = "[^\\u4e00-\\u9fa5]+"
val tmp = arg.replaceAll(regex, "")
if(tmp.length ==0) throw new IllegalArgumentException
tmp
}
}

测试代码:

package com.cyclone
import org.junit.Test
class InitialTest {

@Test
def init():Unit = {
val str = "中华 @ $人名绿*树"
import com.cyclone.Initials.{chineseString2Initials,chineseString2Letters}
println(chineseString2Letters(str))
println(chineseString2Initials(str))
}
}

测试结果:


image.png

你可能感兴趣的:(中文字符串拼音转化和首字母获取demo)