aspose file tools*
The moose likes Beginning Java and the fly likes Find the Comma(,) in sentence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Find the Comma(,) in sentence" Watch "Find the Comma(,) in sentence" New topic
Author

Find the Comma(,) in sentence

saiyalini thangadurai
Greenhorn

Joined: Jun 18, 2010
Posts: 3
Hi,


I need to find comma(,) in a sentence and to replace that comma with any other special characters


please assist me to do this....


Thanks
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
1) Get the sentence as a String.
2) Use the String class' replace/replaceAll methods to replace the comma with another character.

Please post whatever you have got so far, that will help in sorting out problems
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Use String.contains() method to find your ',' char

and as Sridhar told can use raplace or replaceAll method to replace ',' with any char you want..

kaustubh


No Kaustubh No Fun, Know Kaustubh Know Fun..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Kaustubh G Sharma wrote:Use String.contains() method to find your ',' char

In this case contains is useless. It will only tell you if the string contains a comma, not where it is located. indexOf is much better. But replace, replaceAll and replaceFirst (if you only need to replace the first ,) are better alternatives.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Do you want to find the commas, or to just replace them?

If you want to replace them, you can use the String.replace() method

You should have a look at the String API as there are several useful items on there that you may end up using on a daily basis!

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.



Have a look at this, it will replace commas with a question mark :



Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Careful; that is very close to giving too much of an answer.
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Sorry, was being too helpful. Edited my post to remove free cake
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

You can use the method indexof if you need the location.



This method is usefull if you need to now like a file extension or validate if the user inputed data.

If you need to replace, just user the replace/replaceAll and it will do all the job you need.

If you just need to now if the comma is there you may use the contains()


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
James Elsey wrote:Sorry, was being too helpful. Edited my post to remove free cake
No problem.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Hebert Coelho wrote:This method is usefull if you need to now like a file extension...

In the file extension example, would using "split" on the "." be worthwhile, or would it be pointless unless you also want to capture the path?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Kaustubh G Sharma wrote:
Please post more than a single smilie. You have already been told off for meaningless posts, in another thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I earlier wrote:Please post more than a single smilie. . . .
And that doesn't mean post




or similar
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Aye Aye Captain
 
 
subject: Find the Comma(,) in sentence