Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find the Comma(,) in sentence

 
saiyalini thangadurai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :


 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful; that is very close to giving too much of an answer.
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, was being too helpful. Edited my post to remove free cake
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Elsey wrote:Sorry, was being too helpful. Edited my post to remove free cake
No problem.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote:Please post more than a single smilie. . . .
And that doesn't mean post




or similar
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aye Aye Captain
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic