Replace dot in a String.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Replace dot in a String.

    I want to replace dot in a String.
    I used String.replaceA ll ..but due to regular expression .... I couldn't find out the solution.
    I used String.replaceA ll(".","somethi ng");
    But in regular expression "." has a special meaning .....

    Debasis Jana
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    A backslash removes any special meaning from regular expression (meta) characters
    so feed it a \. (backslash dot). Note that if you want to construct a literal String
    for that you need to escape the backslash again to keep javac happy, so you
    need to type "\\." (backslash backslash dot).

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      I think I've posted the following comment a hundred times in various forms, but here goes #101: don't let String's replaceAll blind you to the existence of String's replace: it is a simpler version that doesn't involve regular expressions, so you don't have to wring your hands with worry about special regular expression characters. Calling it is easy:
      [CODE=Java]String result =s.replace(".", "something" );[/CODE]
      Yes, it does replace *all* instances of '.' with 'something':
      Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

      Comment

      Working...