package com.zswc.creativity.utils

import android.os.Bundle
import java.util.*

/**
 *Created by shihuiyun
 *on 2020/10/10
 */
 object StringUtils {


    fun isEmpty(str: String?): Boolean {
        return null == str || "" == str
    }

    /**
     * 剔除参数
     *
     * @param url
     *
     * @return
     */
    fun getPureUrl(url: String): String? {
        var url = url
        if (url.contains("?")) {
            url = url.substring(0, url.indexOf("?"))
        }
        return url
    }

    /**
     * 把url中所含有的参数转换为bundle对象
     *
     * @param url
     *
     * @return
     */
    fun getUrlBundle(url: String): Bundle? {
        val params = getParams(url)
        val iterator = params.keys.iterator()
        val bundle = Bundle()
        while (iterator.hasNext()) {
            val next = iterator.next()
            bundle.putString(next, params[next])
        }
        return bundle
    }

    /**
     * 获取url中的所有参数
     *
     * @param url
     *
     * @return
     */
    fun getParams(url: String): Map<String, String> {
        var url = url
        if (url.contains("&") && url.contains("?")) {
            url = url.substring(url.indexOf("?") + 1, url.length)
        } else if (url.contains("?")) {
            url = url.substring(url.indexOf("?") + 1, url.length)
        }
        val params = url.split("&".toRegex()).toTypedArray()
        val map: MutableMap<String, String> = HashMap()
        for (param in params) {
            val name = param.split("=".toRegex()).toTypedArray()[0]
            var value: String = try {
                param.split("=".toRegex()).toTypedArray()[1]
            } catch (e: Exception) {
                ""
            }
            map[name] = value
        }
        return map
    }

}
