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.
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.
Try with:
String input = "abc/def/ghi/jkl/mno/pqr";
String[] output = input.split("/");
for (int i = 0; i < output.length; i++) {
output[i] += "/";
}
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.
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.
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 . . .