Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chat filtering

 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a method that can do some filtering and manipulations on a String intended for chat client.

The method main purpose is to recognize commands and returns the appropriate output and do some string replacements.

Here what I have come up with so far:

This seems to work a bit but it has limitations and performance hit in String replace methods.

I want to do it using better and efficient approach, I have looked at Pattern and Matcher replacement methods but don't seems to figure it out with this context.

Further I would like to pass for example: "/me grins" and get "Ahmed grins"

Any ideas would be appreciated.
Thanks in advance.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ahmed Yehia:
I want to do it using better and efficient approach, I have looked at Pattern and Matcher replacement methods but don't seems to figure it out with this context.

Further I would like to pass for example: "/me grins" and get "Ahmed grins"


You're on the right track.
Read the docs over again and give it a try - you should be able to reimplement what you have with them. Once you have that it should scale better.

java.util.regex.Pattern
java.util.regex.Matcher
java.util.Scanner

I found myself recently using

java.lang.String.replaceAll() - which is shorthand for the above.

 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one seems more logical:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic