Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 789515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:32:11+00:00 2026-05-14T21:32:11+00:00

I have a string like foo..txt and I want to convert it to foo.txt

  • 0

I have a string like foo..txt and I want to convert it to foo.txt
The occurence of ‘.’ may be more than 2 also.
What is the best way to accomplish this?

edit : The ‘.’ may not occur just together. The occurences may be as below too

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:32:12+00:00Added an answer on May 14, 2026 at 9:32 pm

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

    If so, 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 the way this works is:

    • First, find the lastIndexOf('.') in our string
      • Say this index is k, then we have logically separated our string into:
        • substring(0, k), the prefix part
        • substring(k), the suffix (file extension) part
    • Then we use regex on the prefix part to replaceAll matches of \.+ with " "
      • That is, a literal dot \., repeated one or more times +
      • We also trim() this string to remove leading and trailing spaces
    • The result we want is the transformed prefix concatenated with the original suffix

    Clarifications

    • The reason why the pattern is \.+ instead of .+ is because the dot . is a regex metacharacter, but in this case we really mean a literal period, so it needs to be escaped as \.
    • The reason why this pattern as a Java string literal is "\\.+" is because \ is itself a Java string literal escape character. For example, the string literal "\t" contains the tab character. Analogously, the string literal "\\" contains the backslash character; it has a length() of one.

    References

    • regular-expressions.info/The Dot Matches (Almost) Any Character and Repetition with Star and Plus
    • String API: lastIndexOf and trim()
    • JLS 3.10.6 Escape Sequences for Character and String Literals
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string of text like so: var foo = FooBar; I want
I have a string like this: <span style=font-weight: bold;>Foo</span> I want to use PHP
Say if I have a string like this char foo[10] = %r1%r2; I want
If I have a string like this: FOO[BAR] I need a generic way to
I have a string like this: Name foo Modified bar I want to split
I have a string like this: abc=foo&def=%5Basf%5D&xyz=5 How can I convert it into a
Suppose you have a string like €foo\xA0 , encoded UTF-8, Is there a way
I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
I have a class like so: [JsonObject(MemberSerialization.OptIn)] public class foo { [JsonProperty(name_in_json)] public string
Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.