What is the best way to remove multiple occurrences of a character in a string in java

I have a string like foo..txt and I want to convert it to foo.txt The occurrence of '.' maybe more than 2. What is the best way to do this?

edit: "." may not happen only together. Entries may also be lower:

foo.bar.txt = foo bar.txt
foo..bar.foo.txt = foo bar.txt

+4
source share
6 answers

I believe that you want to replace all periods in the file name part with spaces, but keep the extension, right?

If so, then something like this would be appropriate:

String[] tests = { "foo.bar.txt", // [foo bar.txt] "foo...bar.foo.txt", // [foo bar foo.txt] "........", // [.] "...x...dat", // [x.dat] "foo..txt", // [foo.txt] "mmm....yummy...txt" // [mmm yummy.txt] }; for (String test : tests) { int k = test.lastIndexOf('.'); String s = test.substring(0, k).replaceAll("\\.+", " ").trim() + test.substring(k); System.out.println("[" + s + "]"); } 

Essentially how it works:

  • First find lastIndexOf('.') In our line
    • Let's say that this index is k , then we logically divided our line into:
      • substring(0, k) , prefix part
      • substring(k) , part of the suffix (file extension)
  • Then we use the regular expression in the prefix part to replaceAll matches \.+ With " "
    • That is, the literal dot \. repeated one or more times +
    • We also trim() this line to remove leading and trailing spaces
  • We want a converted prefix combined with the original suffix

Explanation

  • The reason the pattern \.+ Instead of .+ Is because the period . is a regular expression metacharacter, but in this case we really mean the literal period, so it should be avoided as \.
  • The reason this Java string literal pattern is "\\.+" Is because \ itself is the escape character of a Java string string. For example, the string literal "\t" contains a tab character. Similarly, the string literal "\\" contains a backslash character; it has length() one.

References

+5
source

With replaceAll() ! Like this:

 string = string.replaceAll("\\.{2,}", ".") 

Note that we had to avoid the period, as it is a special character in regular expressions (and also avoids the backslash for Java). Also pay attention to {2,} , which means "a match if it occurs two or more times."

+11
source

You made me read the manuals :) I solved a more general problem: how to replace any 2+ identical characters one by one with just one character:

 String str = "assddffffadfdd..o"; System.out.println (str.replaceAll("(.)\\1+", "$1")); 

Conclusion:

 asdfadfd.o 

If you only need a solution for the case of "filename .... ext", then I would prefer something simpler, as in Etaouin's answer, because it probably works faster (but not fact). My solution, simplified for this particular case, is as follows:

 str.replaceAll("(\\.)\\1+", "$1") 
+4
source
 "file....txt".replaceAll("\\.\\.+",".") 

Regex that matches all occurrences of more than one point and replaces it with one point.

0
source

use replaceAll() as follows:

 string = string.replaceAll("\\.+(?=.*\\..*)", " ") 

To paraphrase a regex from left to right:

  • "\\.+" Find one or more periods
  • "(?=.*\\..*)" after viewing and searching for a period

It handles the test case that you mentioned - for example, cases like:

  • Txt.
  • test..txt
  • .test.txt

are converted as follows:

  • Txt.
  • test.txt
  • test.txt
0
source

I suggest String.replaceAll .

-1
source

Source: https://habr.com/ru/post/1309644/


All Articles