singularity
singularity

Reputation: 211

Getting rid of punctuations in Strings

I know that there are a few shorthand ways to go about this in Java, such as

s = s.replaceAll("\\p{Punct}", "");

but I've been instructed to create a method that would get rid of punctuations without using such kinds of ~1 line shortcuts.

This is what I've attempted so far—

      private String removePunctuation( String s )
      {
         //StringBuilder sb = new StringBuilder(s);
         String resultString = ""; 
         String punctuations = "!@#$%^&*()~?,.:;\"'";
         for(int i = 0; i<s.length()-1; i++)
         {
            String sub = s.substring(i, i+1);
            if(punctuations.indexOf(sub) == -1) 
            {
               //sb.deleteCharAt(i); 
               resultString += s.substring(i, i+1); 
            }
         }
         //String resultString = sb.toString(); 
         return resultString; 
      }

However, while it does run without "errors", it turns out that my strings are not changed and still contain punctuations within them. What might I be doing wrong?

Upvotes: 0

Views: 132

Answers (3)

Ryan
Ryan

Reputation: 835

This would be my approach to the problem. Note: I did not test it fully, so don't just copy and paste.

private String removePunctuation( String s )
      {
         StringBuilder sb = new StringBuilder(s);
         String punctuations = "!@#$%^&*()~?,.:;\"'";
         for(int i = 0; i < sb.length(); i++)
         {
            if(punctuations.contains(String.valueOf(sb.charAt(i)))) 
            {
               sb.deleteCharAt(i);
               i--; //Go back 1 index since we just deleted one
            }
         }
         String resultString = sb.toString(); 
         return resultString; 
      }

Upvotes: 0

user2711811
user2711811

Reputation:

Other answers undoubtedly will improve on your approach but your code works other than last character - how are you verifying results? This displays hello world. To fix the last character just modify your for loop - substring is exclusive.

This is your code with only the for loop changed - and it works so post how you are verifying your results.

  public class HelloWorld
  {
    public static void main(String[] args)
    {
      String s = removePunctuation("^^^&*())))!!!?%%!!?~~..he;;':llo!! '''@#$wo...;\"rld");
      System.out.println(s);
    }

  private static String removePunctuation( String s )
        {
           //StringBuilder sb = new StringBuilder(s);
           String resultString = ""; 
           String punctuations = "!@#$%^&*()~?,.:;\"'";
           for(int i = 0; i<s.length(); i++)
           {
              String sub = s.substring(i, i+1);
              if(punctuations.indexOf(sub) == -1) 
              {
                 //sb.deleteCharAt(i); 
                 resultString += s.substring(i, i+1); 
              }
           }
           //String resultString = sb.toString(); 
           return resultString; 
        }
  }

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

There are multiple ways to solve this problem but your use of successive substring is incorrect (and unnecessary). Instead, create an empty StringBuilder and iterate each index of your input String. Take the character at that index and then check that it is not present in your set of punctuations. Assuming it isn't, append it to the StringBuilder. When done with iteration, return the StringBuilder as a String. Like,

private String removePunctuation(String s) {
    StringBuilder sb = new StringBuilder();
    String punctuations = "!@#$%^&*()~?,.:;\"'";
    for (int i = 0; i < s.length(); i++) {
        char ch = s.charAt(i);
        if (punctuations.indexOf(ch) < 0) {
            sb.append(ch);
        }
    }
    return sb.toString();
}

Upvotes: 2

Related Questions