package com.xty.common

import java.util.*
import java.util.regex.Matcher
import java.util.regex.Pattern

class TextUtils {

    /**
     * 检查电话号码
     */
    fun isPhoneNum(phone: String): Boolean {

        if (phone.isNullOrEmpty()) return false

        val pattern = "(13|14|15|16|17|18|19)[0-9]{9}"

        val compile = Pattern.compile(pattern)
        val matcher = compile.matcher(phone)
        return matcher.matches()
    }


    /**
     * 检查身份证信息
     */
    fun isIDCard(idCard: String?): Boolean {
        if (idCard.isNullOrEmpty()) return false

        val pattern = "\\\\d{17}[\\\\d|x]|\\\\d{15}"

        val r = Pattern.compile(pattern)
        val m: Matcher = r.matcher(idCard)
        return m.matches()
    }

    /**
     * 通过身份证号码计算年龄
     */
    fun byIdCardToAge(idcard: String): Int {

        if (!isIDCard(idcard)) {
            return 0
        }

        val selectYear = Integer.valueOf(idcard.substring(6, 10))         //出生的年份
        val cal = Calendar.getInstance()
        return cal.get(Calendar.YEAR) - selectYear
    }


}