Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove special characters from input text
    primarykey
    data
    text
    <p>I want to remove all special characters from input text as well as some restricted words.</p> <p>Whatever the things I want to remove, that will come dynamically </p> <p>(Let me clarify this: Whatever the words I need to exclude they will be provided dynamically - the user will decide what needs to be excluded. That is the reason I did not include regex. restricted_words_list (see my code) will get from the database just to check the code working or not I kept statically ), </p> <p>but for demonstration purposes, I kept them in a String array to confirm whether my code is working properly or not.</p> <pre><code>public class TestKeyword { private static final String[] restricted_words_list={"@","of","an","^","#","&lt;","&gt;","(",")"}; private static final Pattern restrictedReplacer; private static Set&lt;String&gt; restrictedWords = null; static { StringBuilder strb= new StringBuilder(); for(String str:restricted_words_list){ strb.append("\\b").append(Pattern.quote(str)).append("\\b|"); } strb.setLength(strb.length()-1); restrictedReplacer = Pattern.compile(strb.toString(),Pattern.CASE_INSENSITIVE); strb = new StringBuilder(); } public static void main(String[] args) { String inputText = "abcd abc@ cbda ssef of jjj t#he g^g an wh&amp;at ggg&lt;g ss%ss ### (()) D^h^D"; System.out.println("inputText : " + inputText); String modifiedText = restrictedWordCheck(inputText); System.out.println("Modified Text : " + modifiedText); } public static String restrictedWordCheck(String input){ Matcher m = restrictedReplacer.matcher(input); StringBuffer strb = new StringBuffer(input.length());//ensuring capacity while(m.find()){ if(restrictedWords==null)restrictedWords = new HashSet&lt;String&gt;(); restrictedWords.add(m.group()); //m.group() returns what was matched m.appendReplacement(strb,""); //this writes out what came in between matching words for(int i=m.start();i&lt;m.end();i++) strb.append(""); } m.appendTail(strb); return strb.toString(); } } </code></pre> <p>The output is :</p> <p>inputText : abcd abc@ cbda ssef of jjj t#he g^g an wh&amp;at ggg <p>Modified Text : abcd abc@ cbda ssef jjj the gg wh&amp;at gggg ss%ss ### (()) DhD </p> <p>Here the excluded words are <strong>of</strong> and <strong>an</strong>, but only some of the special characters, not all that I specified in <code>restricted_words_list</code> </p> <hr> <p>Now I got a better Solution:</p> <pre><code> String inputText = title;// assigning input List&lt;String&gt; restricted_words_list = catalogueService.getWordStopper(); // getting all stopper words from database dynamically (inside getWordStopper() method just i wrote a query and getting list of words) String finalResult = ""; List&lt;String&gt; stopperCleanText = new ArrayList&lt;String&gt;(); String[] afterTextSplit = inputText.split("\\s"); // split and add to list for (int i = 0; i &lt; afterTextSplit.length; i++) { stopperCleanText.add(afterTextSplit[i]); // adding to list } stopperCleanText.removeAll(restricted_words_list); // remove all word stopper for (String addToString : stopperCleanText) { finalResult += addToString+";"; // add semicolon to cleaned text } return finalResult; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload