0

I have a String like:

line = Microsoft SharePoint Workspace Audit Service  "C:\Program Files\Microsoft Office\Office14\GROOVE.EXE";

I need to split above string in two parts like:

part1=Microsoft SharePoint Workspace Audit Service
part2="C:\Program Files\Microsoft Office\Office14\GROOVE.EXE"

This split is because there are two or more consecutive spaces between those parts.

How to do that?

Krease
  • 15,805
  • 8
  • 54
  • 86
Ankur
  • 11
  • Is there a logic involved here? or do you just want to split at the double quotes? – Ravi Y Jan 04 '13 at 07:17
  • @ryadavilli - the question is how to split at 2 or more consecutive spaces – Krease Jan 04 '13 at 07:21
  • You could combine the answers from http://stackoverflow.com/questions/8966884/regular-expression-checking-for-two-consecutive-spaces and http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters to split based on regex representing two or more consecutive spaces – Krease Jan 04 '13 at 07:31
  • you can probably run a loop to check the maximum length of the spaces occuring continuously and with that length you can try splitting it just in the normal way – Arunkumar Srisailapathi Jan 04 '13 at 07:33

5 Answers5

3
String[] output = line.split("  ");

output[0] and output[1] is your answer

Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98
1
String line = "Microsoft SharePoint Workspace Audit Service  \"C:\\Program Files\\Microsoft Office\\Office14\\GROOVE.EXE\"";

String[] parts = line.split("\\s{2,}");
String part1 = parts[0];
String part2 = parts[1];

A good tutorial for using regular expressions in Java can be found here: http://docs.oracle.com/javase/tutorial/essential/regex/

ReynardC
  • 71
  • 2
  • 5
0

You need regular expression for same.
Try following

String[] output = line.split("  +");

output[0] and output[1] is your answer

Vishwanath
  • 6,284
  • 4
  • 38
  • 57
0

String's method split() receives regex so you can use (for example):

String line = "Microsoft SharePoint Workspace Audit Service  \"C:\\Program Files\\Microsoft Office\\Office14\\GROOVE.EXE\"";
String[] result = line.split("\\s{2}");
Dedyshka
  • 421
  • 3
  • 10
  • 20
0
parts = line.split("\\s\\s+",2);
part1 = parts[0]; 
part2 = parts[1];

Above is working

Ankur
  • 13
  • 2