java 删除字符串中的特定字符

    /**
     * Delete any character in a given String.
     * @param inString the original String
     * @param charsToDelete a set of characters to delete.
     * E.g. "az\n" will delete ‘a‘s, ‘z‘s and new lines.
     * @return the resulting String
     */
    public static String deleteAny(String inString, String charsToDelete) {
        if (!hasLength(inString) || !hasLength(charsToDelete)) {
            return inString;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < inString.length(); i++) {
            char c = inString.charAt(i);
            if (charsToDelete.indexOf(c) == -1) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
    /**
     * Check that the given String is neither {@code null} nor of length 0.
     * Note: Will return {@code true} for a String that purely consists of whitespace.
     * @param str the String to check (may be {@code null})
     * @return {@code true} if the String is not null and has length
     * @see #hasLength(CharSequence)
     */
    public static boolean hasLength(String str) {
        return hasLength((CharSequence) str);
    }
    /**
     * Check that the given CharSequence is neither {@code null} nor of length 0.
     * Note: Will return {@code true} for a CharSequence that purely consists of whitespace.
     * <p><pre class="code">
     * StringUtils.hasLength(null) = false
     * StringUtils.hasLength("") = false
     * StringUtils.hasLength(" ") = true
     * StringUtils.hasLength("Hello") = true
     * </pre>
     * @param str the CharSequence to check (may be {@code null})
     * @return {@code true} if the CharSequence is not null and has length
     * @see #hasText(String)
     */
    public static boolean hasLength(CharSequence str) {
        return (str != null && str.length() > 0);
    }
时间: 2024-08-11 14:39:28

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

java去除字符串中的特定字符

public static void updateFileNames(String url, String index){ File file = new File(url); //判断文件目录是否存在,且是文件目录,非文件 if(file.exists() && file.isDirectory()){ File[] childFiles = file.listFiles(); String path = file.getAbsolutePath(); for(File childFil

[算法]删除字符串中重复的字符

如何删除字符串中重复的字符 问题描述: 删除字符串中重复的字符,例如,"good"去掉重复的字符串后就变成"god". 第一种方法: "蛮力法",最简单的方法就是把这个字符串看作是一个字符数组,对该数组使用双重循环进行遍历,如果发现有重复的字符,就把该字符置为'\0',最后再把这个字符数组中所有的'\0'去掉,此时得到的字符串就是删除重复字符后的目标字符串. 代码如下: package 删除字符串中重复的字符; public class Solu

[两个指针]删除字符串中指定的字符

删除字符串中指定的字符 输入 char *a = "abc123"; char *del = "a13"; 利用两个字符指针的方式,pslow,pfast; char *pslow,*pfast; 两个指针一开始都指向字符串的开始位置; pfast开始遍历字符串, if(*pfast==指定字符){////这里疑惑的地方就是,pslow什么时候向前滑行 将pfast所指字符,移到pslow的位置(就是赋值操作,*pslow = *fast) pslow++; pfa

javascript删除字符串中重复的字符

javascript删除字符串中重复的字符:本章节介绍一下如何删除一个字符串中重复的字符,先不管有没有实际价值,就当做是一种对算法的学习也是挺不错的.代码如下: function dropRepeat(str){ var result=[]; var hash={}; for(var i=0, elem; i<str.length;i++){ elem=str[i]; if(!hash[elem]){ hash[elem]=true; result=result+elem; } } return

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

前段时间接触的这个题目,大体理解了,还有些小地方仍待进一步品味,暂且记下. import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /* * 查找字符串中出现频率最高的字符 * * 主要思路:先将字符

c之PAT刷体--字符串-01--从字符串中找到特定字符

字符串-01. 在字符串中查找指定字符(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 白洪欢(浙江大学) 输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出"Not found":若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例

Java 实例 - 删除字符串中的一个字符

package string; public class deleteString { /** * 删除字符串 * @param args */ public static void main(String[] args) { String str = "helloo,this is my third blog"; String str1 = removeCharAt(str, 5); System.out.println(str1); } public static String r

JAVA校验字符串中的每个字符是否都在GB2312字符集中

需要在服务器端校验前台表单中传来的字符串,是否符合GB2312编码(所有字符都在GB2312字符集). import java.io.UnsupportedEncodingException; public class EncodingValidationUtil { /** * 校验目标字符串中的字符是否均在GB2312字符集 * @param str * @return */ public static boolean isGB2312(String str) { if(str == nul

删除字符串中重复的字符

描述 分析 方法一,蛮力法.两个循环,大循环每次从数组中取出一个字符,小循环重新遍历该数组是否含有该字符.方法二:排序法.对两个字符串的字符进行排序,再比较.方法三:空间换时间.acsII共256个字符.使用256bit记录每个字符是否已出现过.遍历字符串,若已出现过则将该字符替换为'\0'方法四:正则表达式."(?s)(.)(?=.*\\1)" 代码 方法三,空间换时间. public class Test { public static String removeDuplucate