• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

How to add one more space to a string in Java?

Hi,
I have a string in Java like the following:

myfile.c 1.2.3.4.5

I want to put one more space between c and 1. So it will look like this:

s dir1/dir2/dir3/filename.c 1.1.4.2.2.1 sometext # sometext

The general rules for this case is:

One character Space(s) Some directories filename.c ONESPACECHARACTER somenumbers some space(s) and and some text # sometext

I want to add one more space to ONESPACECHARACTER.

How can I do it?

Thanks,
0
Tolgar
Asked:
Tolgar
  • 4
  • 2
2 Solutions
 
CEHJCommented:
Try
s = s.replaceAll("(\\.\\w{1,3}) ", "$1  ");

Open in new window

0
 
CEHJCommented:
Or safer maybe
s = s.replaceAll("(\\.[A-Za-z]{1,3}) ", "$1  ");

Open in new window

0
 
cmalakarCommented:
String s ="myfile.c1.2.3.4.5";
System.out.println(s.replaceFirst("([^.].c)(.*)", "$1 $2"));
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cmalakarCommented:
System.out.println(s.replaceFirst("([^.]\\.c)(.*)", "$1 $2"));
0
 
CEHJCommented:
This is safer still:


s = s.replaceAll("(\\.[A-Za-z]{1,3}) (\\d)", "$1  $2");

Open in new window

0
 
CEHJCommented:
:)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now