Home > Enterprise >  Removing specific characters from the elements in an array
Removing specific characters from the elements in an array

Time:12-07

public static String[] removeCharacters(String[] arr){


    String[] str = new String[arr.length];

    for (int i = 0; i <arr.length; i  ) {
        if(Character.isLetter(arr[i].charAt(i)))
            str[i]  = arr[i].charAt(i);
        else
            str[i] = "";
    }
    return str;
}

I'm trying to write a method that returns to String array and takes an String array argument. I need remove special characters from the element and return the element without specials.

  • For example; ["Hel123&$$o", "#$%World", "###"] will return to
  • ["Hello", "World", "",] as a output.
  • I also converted back to String, remove those special characters and split back again to an Array but when i convert to String it all comes together and there is no split point and I don't want to do it with Regex so i'm trying to do without it. With my solution I'm getting null so i couldn't fix.

CodePudding user response:

Here's a changed version. You have to use a nested for loop - the first iterates over the input String[] and the nested one over the char[] in each string.

I have also used some variables in place of repeated array references for easy reading.

public class StrArray{
    public static void main( String[] args ){
        String[] input = new String[]{ "Hel123&$$o", "#$%World", "###" };
        String[] strArr = removeCharacters( input );
        Arrays.stream( strArr ).forEach( s -> System.out.println( "'"   s   "'" ) );
    }

    public static String[] removeCharacters( String[] arr ){
        String[] str = new String[ arr.length ];

        for( int i = 0; i < arr.length; i   ){
            String input = arr[ i ];
            
            /* If the input string in empty, skip.*/
            int k = 0;
            if( input == null || input.length() == 0 )
                str[ i ] = "";
            else{
                /* Construct a char[] containing all the relevant chars. */
                char[] chars = new char[ input.length() ];
                for( int j = 0; j < input.length(); j   ){
                    char c = input.charAt( j );
                    if( Character.isLetter( c ) ) chars[ k   ] = c;
                }

                /* Now, convert the char[] into a string. */
                str[ i ] = String.valueOf( chars, 0, k );
            }
        }

        return str;
    }
}

I have quoted each string while printing. Hence, running this gives the following output.

'Helo'
'World'
''

CodePudding user response:

The problem is that you are iterating over the array of strings but not over the array of characters of each string. You can use a loop to iterate over the array of Strings and a nested loop to iterate over the characters of each String.

Here I'm using StringBuilder to concatenate the Strings since it is more efficient than using . This is because the String object is immutable, so each call for concatenation will create a new String object. On the other hand, StringBuilder is a mutable array of characters.

import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {

        String[] stringArray = 
        new String[]{ "Hell123&$$o", "#$%World", "###" };
        System.out.println(
        Arrays.toString(removeCharacters(stringArray)));
    }

    public static String[] removeCharacters(String[] arr) {
        String[] str = new String[arr.length];

        for (int i = 0; i < arr.length; i  ) {
            StringBuilder strBuilder = new StringBuilder();
            for (int j = 0; j < arr[i].length(); j  ) {
                if (Character.isLetter(arr[i].charAt(j)))
                    strBuilder.append(arr[i].charAt(j));
            }
            str[i] = strBuilder.toString();
        }
        return str;
    }
}
  • Related