Java.Utils:字符串工具类

Don’t say much, just go to the code.

<!-- commons -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>
<!-- hutool -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.1</version>
</dependency>
package org.bood.common.utils;

import cn.hutool.core.text.StrFormatter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;

/**
 * 字符串工具类,提供了字符串的常用操作方法
 *
 * @author bood
 * @since 2024/01/05
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    private StringUtils() {
    }

    /**
     * 判断一个字符串是否为空
     *
     * @param str 待判断的字符串
     * @return boolean
     * @author bood
     * @since 2024/01/05
     */
    public static boolean isEmpty(String str) {
        return str == null || str.trim().isEmpty();
    }

    /**
     * 判断一个字符串是否非空
     *
     * @param str 待判断的字符串
     * @return boolean
     * @author bood
     * @since 2024/01/05
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 判断一个集合是否为空
     *
     * @param coll 待判断的集合
     * @return boolean
     * @author bood
     * @since 2024/01/05
     */
    public static boolean isEmpty(Collection<?> coll) {
        return coll == null || coll.isEmpty();
    }

    /**
     * 判断一个集合是否非空
     *
     * @param coll 待判断的集合
     * @return boolean
     * @author bood
     * @since 2024/01/05
     */
    public static boolean isNotEmpty(Collection<?> coll) {
        return !isEmpty(coll);
    }

    /**
     * 判断一个 Map 是否为空
     *
     * @param map 待判断的 Map
     * @return boolean
     * @author bood
     * @since 2024/01/05
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    /**
     * 判断一个 Map 是否非空
     *
     * @param map 待判断的 Map
     * @return boolean
     * @author bood
     * @since 2024/01/05
     */
    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    /**
     * 下划线转驼峰命名
     *
     * @param s 待转换的下划线命名字符串
     * @return {@link String }
     * @author bood
     * @since 2024/01/05
     */
    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }

        s = s.toLowerCase();
        String[] parts = s.split("_");
        StringBuilder camelCaseString = new StringBuilder(parts[0]);

        for (int i = 1; i < parts.length; i++) {
            String part = parts[i];
            camelCaseString.append(toUpperCaseFirstChar(part));
        }
        return camelCaseString.toString();
    }

    /**
     * 首字母转大写
     *
     * @param str 待转换的字符串
     * @return {@link String }
     * @author bood
     * @since 2024/01/05
     */
    public static String toUpperCaseFirstChar(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    /**
     * 将字符串的首字母转换为小写
     *
     * @param str 待转换的字符串
     * @return {@link String }
     * @author bood
     * @since 2024/01/05
     */
    public static String uncapitalize(String str) {
        if (isEmpty(str)) {
            return str;
        }
        char firstChar = str.charAt(0);
        if (Character.isLowerCase(firstChar)) {
            return str;
        }
        return Character.toLowerCase(firstChar) + str.substring(1);
    }

    /**
     * 删除字符串中的所有空格,包括中间的空格
     *
     * @param str 待处理的字符串
     * @return {@link String }
     * @author bood
     * @since 2024/01/05
     */
    public static String deleteWhitespace(String str) {
        if (isEmpty(str)) {
            return str;
        }
        return str.replace(" ", "");
    }

    /**
     * 格式化字符串,将占位符{}替换为实际参数
     *
     * @param template 文本模板,包含占位符
     * @param params   参数列表
     * @return {@link String }
     * @author bood
     * @since 2024/01/05
     */
    public static String format(String template, Object... params) {
        if (isEmpty(template) || isEmpty(Arrays.asList(params))) {
            return template;
        }
        return StrFormatter.format(template, params);
    }

    /**
     * 反转字符串
     *
     * @param str 需要反转的字符串
     * @return {@link String }
     * @author bood
     * @since 2024/01/05
     */
    public static String reverse(String str) {
        return new StringBuilder(str).reverse().toString();
    }

    /**
     * 判断字符串是否匹配指定的正则表达式
     *
     * @param str   源字符串
     * @param regex 正则表达式
     * @return boolean
     * @author bood
     * @since 2024/01/05
     */
    public static boolean matches(String str, String regex) {
        if (str == null || regex == null) {
            return false;
        }
        return str.matches(regex);
    }

    /**
     * 计算子字符串在父字符串中出现的次数
     *
     * @param str    父字符串
     * @param subStr 子字符串
     * @return int
     * @author bood
     * @since 2024/01/05
     */
    public static int countMatches(String str, String subStr) {
        if (isEmpty(str) || isEmpty(subStr)) {
            return 0;
        }
        int count = 0;
        int idx = 0;
        while ((idx = str.indexOf(subStr, idx)) != -1) {
            count++;
            idx += subStr.length();
        }
        return count;
    }

    /**
     * URL编码
     *
     * @param str     待编码的字符串
     * @param charset 编码格式
     * @return {@link String }
     * @author bood
     * @since 2024/01/05
     */
    public static String encodeUrl(String str, String charset) {
        if (str == null) {
            return null;
        }
        try {
            return URLEncoder.encode(str, charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unsupported Encoding Exception", e);
        }
    }

    /**
     * URL解码
     *
     * @param str     待解码的字符串
     * @param charset 解码格式
     * @return {@link String }
     * @author bood
     * @since 2024/01/05
     */
    public static String decodeUrl(String str, String charset) {
        if (str == null) {
            return null;
        }
        try {
            return URLDecoder.decode(str, charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unsupported Encoding Exception", e);
        }
    }

    /**
     * 将字符串按指定分隔符转换为Set集合
     *
     * @param str 待分割的字符串
     * @param sep 分隔符
     * @return {@link Set }<{@link String }>
     * @author bood
     * @since 2024/01/05
     */
    public static Set<String> str2Set(String str, String sep) {
        // 此处调用str2List方法并包装为HashSet
        return new HashSet<>(str2List(str, sep, true, false));
    }

    /**
     * 将字符串按指定分隔符转换为List集合
     *
     * @param str         待分割的字符串
     * @param sep         分隔符
     * @param filterBlank 是否过滤空白字符串
     * @param trim        是否去除首尾空白字符
     * @return {@link List }<{@link String }>
     * @author bood
     * @since 2024/01/05
     */
    public static List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) {
        List<String> list = new ArrayList<>();
        if (isEmpty(str)) {
            return list;
        }
        // 若字符串为空或者为blank时,可直接返回空列表
        if (filterBlank && isBlank(str)) {
            return list;
        }
        // 分割字符串
        String[] split = str.split(sep);
        for (String string : split) {
            if (filterBlank && isBlank(string)) {
                continue;
            }
            if (trim) {
                string = string.trim();
            }
            list.add(string);
        }
        return list;
    }
    
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、什么是XSS攻击 XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中。比如这些代码包括HTML代码和客户端脚本。攻击者利用XSS漏洞旁路掉访问控制——例如同源策略(same origin policy)。这种类型的漏洞由于被黑客用来编写危害性更大的网络钓鱼(Phishing)攻击而变得广为人知。对于跨站脚本攻击,黑客界共识是:跨站脚本攻击是新型的“缓冲区溢出攻击“,而JavaScript是新型的“ShellCode”。 二、XSS漏洞的危害 (1)网络钓鱼,包括盗取各类用户账号; (2)窃取用户cookies资料,从而获取用户隐私信息,或利用用户身份进一步对网站执行操作; (3)劫持用户(浏览器)会话,从而执行任意操作,例如进行非法转账、强制发表日志、发送电子邮件等; (4)强制弹出广告页面、刷流量等; (5)网页挂马; (6)进行恶意操作,例如任意篡改页面信息、删除文章等; (7)进行大量的客户端攻击,如DDoS攻击; (8)获取客户端信息,例如用户的浏览历史、真实IP、开放端口等; (9)控制受害者机器向其他网站发起攻击; (10)结合其他漏洞,如CSRF漏洞,实施进一步作恶; (11)提升用户权限,包括进一步渗透网站; (12)传播跨站脚本蠕虫等; 三、过滤器配置 web.xml配置 XssFilter com.xxx.Filter.XssFilter XssFilter /*

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值