-2

I have string which goes like this-

abc/def/ghi/jkl/mno/pqr

Now I want to represent this as-

abc/
def/
ghi/
jkl/
mno/
pqr

I am trying to achieve this using JAVA, can any one please provide me a sample code.

Som Sarkar
  • 289
  • 1
  • 5
  • 24

4 Answers4

2

Try with:

String input = "abc/def/ghi/jkl/mno/pqr";
String[] output = input.split("/");

for (int i = 0; i < output.length; i++) {
  output[i] += "/";
}
hsz
  • 148,279
  • 62
  • 259
  • 315
2

One approach would be to replace each occurrence of "/" with "/\n" (\n is a new line character.)

String provides a replace() method for doing this.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
1

I'll hint you. Have a look at the java docs for String class and find a method for replacing / with /\n in the string.

anirudh
  • 4,116
  • 2
  • 20
  • 35
0

You can use the following code

String strValue="abc/def/ghi/jkl/mno/pqr";
String strValue1=strValue;

System.out.println("Actual Value \n >>>"+strValue);

strValue1=strValue1.replaceAll("/","/\n");

System.out.println("\nstrFormattedValue >>>\n"+strValue1);

The output of the program is as follows:-

Actual Value
 >>>abc/def/ghi/jkl/mno/pqr

strFormattedValue >>>
abc/
def/
ghi/
jkl/
mno/
pqr
Press any key to continue . . .
Prem
  • 91
  • 1
  • 14