1. /**
  2. * Delete any character in a given String.
  3. * @param inString the original String
  4. * @param charsToDelete a set of characters to delete.
  5. * E.g. "az\n" will delete 'a's, 'z's and new lines.
  6. * @return the resulting String
  7. */
  8. public static String deleteAny(String inString, String charsToDelete) {
  9. if (!hasLength(inString) || !hasLength(charsToDelete)) {
  10. return inString;
  11. }
  12. StringBuilder sb = new StringBuilder();
  13. for (int i = 0; i < inString.length(); i++) {
  14. char c = inString.charAt(i);
  15. if (charsToDelete.indexOf(c) == -1) {
  16. sb.append(c);
  17. }
  18. }
  19. return sb.toString();
  20. }
  21. /**
  22. * Check that the given String is neither {@code null} nor of length 0.
  23. * Note: Will return {@code true} for a String that purely consists of whitespace.
  24. * @param str the String to check (may be {@code null})
  25. * @return {@code true} if the String is not null and has length
  26. * @see #hasLength(CharSequence)
  27. */
  28. public static boolean hasLength(String str) {
  29. return hasLength((CharSequence) str);
  30. }
  31. /**
  32. * Check that the given CharSequence is neither {@code null} nor of length 0.
  33. * Note: Will return {@code true} for a CharSequence that purely consists of whitespace.
  34. * <p><pre class="code">
  35. * StringUtils.hasLength(null) = false
  36. * StringUtils.hasLength("") = false
  37. * StringUtils.hasLength(" ") = true
  38. * StringUtils.hasLength("Hello") = true
  39. * </pre>
  40. * @param str the CharSequence to check (may be {@code null})
  41. * @return {@code true} if the CharSequence is not null and has length
  42. * @see #hasText(String)
  43. */
  44. public static boolean hasLength(CharSequence str) {
  45. return (str != null && str.length() > 0);
  46. }

java 删除字符串中的特定字符的更多相关文章

  1. Java-Runoob-高级教程-实例-字符串:03. Java 实例 - 删除字符串中的一个字符

    ylbtech-Java-Runoob-高级教程-实例-字符串:03. Java 实例 - 删除字符串中的一个字符 1.返回顶部 1. Java 实例 - 删除字符串中的一个字符  Java 实例 以 ...

  2. 1231: 删除字符串中指定的字符(Java)

    WUSTOJ 1231: 删除字符串中指定的字符 题目 原题链接 Description 明天就要英语考试了,小明明正在挑灯夜战背单词.小明明发现单词很难背,背一个忘一个.经过仔细研究,小明明发现单词 ...

  3. 三种java 去掉字符串中的重复字符函数

    三种java 去掉字符串中的重复字符函数 public static void main(string[] args) { system.out.println(removerepeatedchar( ...

  4. jst通用删除数组中重复的值和删除字符串中重复的字符

    以下内容属于个人原创,转载请注明出处,非常感谢! 删除数组中重复的值或者删除字符串重复的字符,是我们前端开发人员碰到很多这样的场景.还有求职者在被面试时也会碰到这样的问题!比如:问删除字符串重复的字符 ...

  5. Java 求字符串中出现频率最高字符

    前段时间接触的这个题目,大体理解了,还有些小地方仍待进一步品味,暂且记下. import java.util.ArrayList; import java.util.Arrays; import ja ...

  6. 【自制工具类】Java删除字符串中的元素

    这几天做项目需要把多个item的id存储到一个字符串中,保存进数据库.保存倒是简单,只需要判断之前是否为空,如果空就直接添加,非空则拼接个"," 所以这个字符串的数据结构是这样的 ...

  7. 使用Java判断字符串中的中文字符数量

    Java判断一个字符串str中中文的个数,经过总结,有以下几种方法(全部经过验证),可根据其原理判断在何种情况下使用哪个方法: 1. char[] c = str.toCharArray(); for ...

  8. java 删除字符串中的反斜杠\

    Java中有时候会打印出来会含有反斜杠(\)的字符串,我们需要删除时,可以使用 replace() 或 replaceAll() 但是要注意的是replaceAll()里面用的是正则表达式,所以一个斜 ...

  9. C语言删除字符串中重复的字符

    #include <stdio.h> #include <string.h> #define NR(x) sizeof(x)/sizeof(x[0]) int Del_char ...

随机推荐

  1. PHP curl 采集内容之规则 1

    <?phpheader("Content-type:text/html; charset=utf-8");$pattern = '/xxx(.*)yyyy/isU'; //i ...

  2. Razor与ASPX语法比较

  3. 简易ORM(基于注解)

    这是从我们现有项目做的一定的改进准备做成IDE插件 类似getter和setter的生成 1.定义实体类 通过注解说明其表名和字段名(SOURCE类型的注解 不需要运行时使用)@TableName(& ...

  4. (转载)EhLib 在 Delphi 7 下的安装方法

    EhLib 在 Delphi 7 下的安装方法 1.将 EhLib 解压到一个目录,如:E:\VCL\EhLib: 2.将 EhLib 安装目录下 Common 目录.DataService 目录下的 ...

  5. Django web 开发指南 no such table:

    在学习django web开发指南时,发布新博客点击save后会有error提示:no such table balabalabala... 百度了一下说重新运行manage.py syncdb 就可 ...

  6. python 输出小数控制

    一.要求较小的精度 将精度高的浮点数转换成精度低的浮点数. 1.round()内置方法round()不是简单的四舍五入的处理方式. >>> round(2.5) 2 >> ...

  7. Length of Last Word | Leetcode

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. MYSQL数据库备份与恢复【转】

    mysqldump -h主机名  -P端口 -u用户名 -p密码 (–database) 数据库名 > 文件名.sql  在window上需要通过CMD进入mysql安装目录下的bin目录下执行 ...

  9. centos系统使用技巧

    ===============================网络配置===============================1 eth0 接口配置:/etc/sysconfig/network ...

  10. Eclipse里初次使用Maven注意问题

    在Eclipse里初次使用Maven,右键project->add dependency, 发现search不管用,这时候需要按照以下步骤操作: Goto "Preferences - ...