Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,401 questions

56,365 answers

573 users

How to remove characters present in first string from the second string in Java

2 Answers

0 votes
public class RemoveCharactersProgram { 
    static String removeChars(String s, String chars) { 
        int i = 0, internal_i = 0, len = s.length(); 
        char arr[] = s.toCharArray(); 
          
        while (i != len) { 
            char ch = arr[i]; 
  
            if (chars.indexOf(ch) == -1) { 
                arr[internal_i] = arr[i]; 
                  
                internal_i++; 
            }
            i++; 
        }  
        s = new String(arr); 
          
        return s.substring(0, internal_i); 
    } 
        
    public static void main(String args[]) {
        String first = "programming language"; 
        String second = "rogy"; 
          
        System.out.println(removeChars(second, first)); 
    } 
} 
 
  
/*
run:
  
y
  
*/

 



answered Feb 4, 2019 by avibootz
edited 2 days ago by avibootz
0 votes
import java.util.HashSet;
import java.util.Set;

/**
 * Demonstrates how to remove from one string all characters
 * that appear in another string.
 */
public class RemoveCharactersProgram {

    public static void main(String[] args) {
        String first = "programming language";
        String second = "rogy";

        String result = removeCharactersPresentInFirst(first, second);

        System.out.println("First : " + first);
        System.out.println("Second: " + second);
        System.out.println("Result: \"" + result + "\"");
    }

    /**
     * Removes every character from {@code second} that appears
     * anywhere in {@code first}.
     *
     * Example:
     * first  = "programming language"
     * second = "rogy"
     * result = ""
     *
     * Why use a Set?
     * - O(1) average lookup time per character.
     * - Makes the intent clear and efficient.
     *
     * @param first  String containing characters to exclude
     * @param second String to filter
     * @return A new string containing only characters from second
     *         that do not appear in first
     */
    public static String removeCharactersPresentInFirst(
            String first,
            String second) {

        // Store all characters from the first string for fast lookup.
        Set<Character> excludedChars = new HashSet<>();

        for (char c : first.toCharArray()) {
            excludedChars.add(c);
        }

        // Build the filtered result.
        StringBuilder result = new StringBuilder();

        for (char c : second.toCharArray()) {
            if (!excludedChars.contains(c)) {
                result.append(c);
            }
        }

        return result.toString();
    }
}


/*
run:

First : programming language
Second: rogy
Result: "y"

*/

 



answered 2 days ago by avibootz

Related questions

...