How to remove specific line from multi-line string - java

I have below java string as command output
String output = "NIC Value\n"
+ "------ -----\n"
+ "vmn0 on \n"
+ "vmn1 on \n"
+ "vmn2 on \n"
+ "vmn3 on \n"
+ "vmn4 on";
I want to remove second line with dash from above string. How can I do it?
I tried it using contains method but it is generating blank line after removing second line.
if(output!=null && output.contains("-"))
output = output.replace("-","");

This is complete answer you are looking for:
String output = "NIC Value\n"
+ "------ -----\n"
+ "vmn0 on \n"
+ "vmn1 on \n"
+ "vmn2 on \n"
+ "vmn3 on \n"
+ "vmn4 on";
String str = Stream.of(output.split("\n"))
.filter(s -> !s.contains("--"))
.collect(Collectors.joining("\n"));

You can use this to remove that line and use the result,
String result = output.replace("------ -----\n", "");
It will replace that line with an empty String

Related

Splitting array in string does not give last element

Hi I am splitting and storing string with use of array but does not give result
String str = "123456";
String[] arrOfStr = str.split("");
String otpnum1 = arrOfStr[0];
String otpnum2 = arrOfStr[1];
String otpnum3 = arrOfStr[2];
String otpnum4 = arrOfStr[3];
String otpnum5 = arrOfStr[4];
String otpnum6 = arrOfStr[5];
System.out.println("otp"+otpnum1+otpnum2+otpnum3+otpnum4+otpnum5+otpnum6);
OUTPUT
System.out: otp12345
You are printing without any space or newline, which is the reason you are not able to interpret individual variables. Use this
System.out.println("otp " + otpnum1+ " " + otpnum2+" " + " "+ otpnum3+ " " + otpnum4+ " " + otpnum5+ " " + otpnum6);
I understand, the output is 12345, and expected 123456 for the result.
But, looking your code looks like correct.
I have try your code here, for test, and works fine.
The output was: otp123456

How can I add a newline character to a String in Java?

In a Java application, I am creating a String like below (by concatenation):
String notaCorrente = dataOdierna + " - " + testoNotaCorrente;
My problem is that I want to add also something like an HTML newline character at the end of this String (that will be shown into an HTML page).
How can I implement it?
The newline character in Java is "\n" which will look like this:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "\n";
However, this will not display as you expect on your HTML page. You can try adding an html break tag, or add the
(Line Feed) and 
 (Carriage Return) HTML entities:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
or
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "
&#10";
For a newline that will result in a line break in HTML, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
For a newline that will result in a line break in your text editor, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + System.lineSeparator();
And for both, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>" + System.lineSeparator();
Why not \n?
\n is specific to certain operating systems, while others use \r\n. System.lineSeparator() will get you the one that is relevant to the system where you are executing your application. See the documentation for more info on this function, and Wikipedia for more info on newlines in general.
Simply, need to add <br/> (break line tag of HTML).
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br/>";
so, while you are going to display this content, <br/> tag will rendered on HTML page in form of new line.

why is "+" added to all my coordinates in url builder?

I have this code to build a string of coordinates:
public String generateUrl(CompleteRoutingResponseShort completeRoutingResponse) {
if (completeRoutingResponse == null)
{
return "CompleteRoutingResponse is null";
}
UriBuilder builder = UriBuilder
.fromPath(Constants.LIVEMAP_BASE_URL)
.scheme("http");
builder.queryParam("lineGeom", getCoordsLists(completeRoutingResponse));
return builder.build().toString();
}
private String getCoordsLists() {
List<String> lists = new ArrayList<>();
for (AlternativeShort alternative : completeRoutingResponses.alternatives)
{
String list = "( " + alternative.coords.stream().map(item -> String.format("%.4f , %.4f", item.x, item.y))
.collect(Collectors.joining(", ")) + ")";
// list.replace("+","");
lists.add(list);
}
return String.join(", ", lists);
}
however I get this url as an output:
"http:/myhost.com/tiles/internal?lineGeom=(+34.9178+,+31.9550,+34.9175+,+31.9552,+34.9174+,+31.9553,+34.9172+,+31.9555,+34.9171+,+31.9556,+34.9171+,+31.9556,+34.9168+,+31.9559,+34.9168+,+31.9559,+34.9167+,+31.9561,+34.9165+,+31.9562,+34.9164+,+31.9564,+34.9161+,+31.9567,+34.9161+,+31.9568,+34.9160+,+31.9570,+34.9159+,+31.9571,+34.9158+,+31.9572,+34.9158+,+31.9573,+34.9157+,+31.9574,+34.9157+,+31.9574,+34.9166+,+31.9577,+34.9166+,+31.9577,+34.9178+,+31.9581,+34.9184+,+31.9583,+34.9188+,+31.9583,+34.9192+,+31.9584,+34.9195+,+31.9585,+34.9201+,+31.9585,+34.9204+,+31.9585,+34.9208+,+31.9585,+34.9211+,+31.9585,+34.9214+,+31.9585,+34.9216+,+31.9585,+34.9222+,+31.9584,+34.9224+,+31.9584,+34.9227+,+31.9583,+34.9233+,+31.9582,+34.9233+,+31.9582,+34.9238+,+31.9580,+34.9246+,+31.9577,+34.9257+,+31.9572,+34.9262+,+31.9571,+34.9265+,+31.9569,+34.9266+,+31.9569,+34.9268+,+31.9569,+34.9271+,+31.9568,+34.9276+,+31.9567,+34.9281+,+31.9566,+34.9284+,+31.9566,+34.9289+,+31.9566,+34.9307+,+31.9565,+34.9314+,+31.9564,+34.9320+,+31.9564,+34.9325+,+31.9563,+34.9325+,+31.9563,+34.9330+,+31.9563,+34.9335+,+31.9563,+34.9340+,+31.9562,+34.9345+,+31.9562,+34.9345+,+31.9562,+34.9357+,+31.9560,+34.9361+,+31.9560,+34.9365+,+31.9559,+34.9365+,+31.9559,+34.9369+,+31.9559,+34.9373+,+31.9558,+34.9373+,+31.9558,+34.9376+,+31.9557,+34.9377+,+31.9556,+34.9382+,+31.9554,+34.9387+,+31.9549,+34.9387+,+31.9549,+34.9389+,+31.9547,+34.9394+,+31.9542,+34.9394+,+31.9542,+34.9395+,+31.9543,+34.9395+,+31.9543,+34.9398+,+31.9545,+34.9399+,+31.9546,+34.9399+,+31.9546,+34.9401+,+31.9548,+34.9407+,+31.9555,+34.9414+,+31.9565,+34.9417+,+31.9571,+34.9417+,+31.9571,+34.9418+,+31.9572,+34.9418+,+31.9572,+34.9420+,+31.9578,+34.9420+,+31.9579,+34.9421+,+31.9585,+34.9421+,+31.9585,+34.9421+,+31.9590,+34.9421+,+31.9599,+34.9421+,+31.9599,+34.9422+,+31.9607,+34.9422+,+31.9608,+34.9423+,+31.9609,+34.9423+,+31.9610,+34.9423+,+31.9610,+34.9423+,+31.9611,+34.9424+,+31.9611,+34.9425+,+31.9612,+34.9426+,+31.9613,+34.9426+,+31.9613,+34.9426+,+31.9613,+34.9429+,+31.9616,+34.9432+,+31.9618,+34.9436+,+31.9621,+34.9449+,+31.9630,+34.9449+,+31.9630,+34.9487+,+31.9649,+34.9487+,+31.9649,+34.9525+,+31.9668,+34.9525+,+31.9668,+34.9531+,+31.9671,+34.9535+,+31.9672,+34.9538+,+31.9674,+34.9542+,+31.9676,+34.9546+,+31.9678,+34.9550+,+31.9680,+34.9553+,+31.9682,+34.9554+,+31.9682,+34.9558+,+31.9684,+34.9566+,+31.9689,+34.9570+,+31.9691,+34.9573+,+31.9694,+34.9579+,+31.9698,+34.9582+,+31.9701,+34.9586+,+31.9704,+34.9591+,+31.9709,+34.9594+,+31.9713,+34.9599+,+31.9720,+34.9602+,+31.9725,+34.9605+,+31.9730,+34.9608+,+31.9736,+34.9610+,+31.9742,+34.9612+,+31.9746,+34.9613+,+31.9752,+34.9614+,+31.9756,+34.9615+,+31.9764,+34.9627+,+31.9857,+34.9630+,+31.9883,+34.9632+,+31.9892,+34.9634+,+31.9902,+34.9637+,+31.9912,+34.9654+,+31.9961,+34.9658+,+31.9974,+34.9659+,+31.9979,+34.9660+,+31.9984,+34.9662+,+31.9994,+34.9663+,+32.0002,+34.9664+,+32.0011,+34.9671+,+32.0154,+34.9671+,+32.0166,+34.9671+,+32.0169,+34.9671+,+32.0176,+34.9670+,+32.0179,+34.9669+,+32.0185,+34.9668+,+32.0192,+34.9666+,+32.0198,+34.9663+,+32.0206,+34.9660+,+32.0211,+34.9656+,+32.0218,+34.9652+,+32.0225,+34.9649+,+32.0229,+34.9644+,+32.0236,+34.9627+,+32.0256,+34.9607+,+32.0279,+34.9602+,+32.0285,+34.9596+,+32.0292,+34.9587+,+32.0300,+34.9534+,+32.0351,+34.9530+,+32.0356,+34.9525+,+32.0361,+34.9520+,+32.0368,+34.9515+,+32.0374,+34.9414+,+32.0522,+34.9408+,+32.0531,+34.9404+,+32.0539,+34.9399+,+32.0548,+34.9396+,+32.0555,+34.9392+,+32.0567,+34.9381+,+32.0603,+34.9381+,+32.0603,+34.9357+,+32.0679,+34.9357+,+32.0679,+34.9341+,+32.0730,+34.9341+,+32.0730,+34.9336+,+32.0747,+34.9331+,+32.0765,+34.9331+,+32.0765,+34.9330+,+32.0767,+34.9330+,+32.0768,+34.9328+,+32.0781,+34.9328+,+32.0781,+34.9328+,+32.0793,+34.9328+,+32.0793,+34.9333+,+32.0832,+34.9341+,+32.0899,+34.9351+,+32.0989,+34.9358+,+32.1057,+34.9359+,+32.1064,+34.9359+,+32.1064,+34.9359+,+32.1075,+34.9361+,+32.1093,+34.9363+,+32.1106,+34.9366+,+32.1119,+34.9369+,+32.1130,+34.9369+,+32.1131,+34.9373+,+32.1141,+34.9375+,+32.1146,+34.9381+,+32.1159,+34.9387+,+32.1171,+34.9391+,+32.1178,+34.9391+,+32.1178,+34.9399+,+32.1189,+34.9403+,+32.1195,+34.9410+,+32.1204,+34.9411+,+32.1204,+34.9414+,+32.1208,+34.9442+,+32.1238,+34.9477+,+32.1273,+34.9477+,+32.1273,+34.9497+,+32.1294,+34.9497+,+32.1294,+34.9533+,+32.1331,+34.9533+,+32.1331,+34.9554+,+32.1350,+34.9556+,+32.1351,+34.9558+,+32.1352,+34.9562+,+32.1354,+34.9564+,+32.1355,+34.9566+,+32.1355,+34.9578+,+32.1358,+34.9582+,+32.1360,+34.9586+,+32.1361,+34.9589+,+32.1363,+34.9591+,+32.1364,+34.9593+,+32.1366,+34.9595+,+32.1368,+34.9596+,+32.1370,+34.9598+,+32.1372,+34.9598+,+32.1374,+34.9599+,+32.1377,+34.9600+,+32.1379,+34.9600+,+32.1381,+34.9600+,+32.1383,+34.9599+,+32.1385,+34.9599+,+32.1387,+34.9598+,+32.1389,+34.9597+,+32.1392,+34.9596+,+32.1393,+34.9595+,+32.1395,+34.9594+,+32.1397,+34.9592+,+32.1399,+34.9591+,+32.1400,+34.9589+,+32.1402,+34.9587+,+32.1403,+34.9560+,+32.1421,+34.9560+,+32.1421,+34.9515+,+32.1449,+34.9515+,+32.1449,+34.9513+,+32.1451,+34.9513+,+32.1451,+34.9509+,+32.1453,+34.9509+,+32.1453,+34.9504+,+32.1457,+34.9504+,+32.1457,+34.9456+,+32.1488,+34.9439+,+32.1500,+34.9431+,+32.1505,+34.9431+,+32.1505,+34.9423+,+32.1512,+34.9403+,+32.1527,+34.9399+,+32.1531,+34.9371+,+32.1556,+34.9324+,+32.1598,+34.9297+,+32.1622,+34.9297+,+32.1622,+34.9280+,+32.1635,+34.9265+,+32.1645,+34.9254+,+32.1650,+34.9237+,+32.1658,+34.9237+,+32.1658,+34.9237+,+32.1658,+34.9235+,+32.1659,+34.9223+,+32.1663,+34.9213+,+32.1665,+34.9209+,+32.1666,+34.9207+,+32.1667,+34.9196+,+32.1670,+34.9167+,+32.1676,+34.9167+,+32.1676,+34.9146+,+32.1681,+34.9124+,+32.1686,+34.9121+,+32.1686,+34.9112+,+32.1688,+34.9101+,+32.1689,+34.9089+,+32.1690,+34.9065+,+32.1693,+34.9056+,+32.1694,+34.9045+,+32.1695,+34.9038+,+32.1697,+34.9032+,+32.1698,+34.9031+,+32.1698,+34.9025+,+32.1700,+34.9022+,+32.1701,+34.9018+,+32.1702,+34.9015+,+32.1703,+34.9002+,+32.1708,+34.8985+,+32.1714,+34.8985+,+32.1714,+34.8982+,+32.1714,+34.8981+,+32.1714,+34.8977+,+32.1714,+34.8975+,+32.1714,+34.8972+,+32.1714,+34.8970+,+32.1714,+34.8963+,+32.1716,+34.8938+,+32.1721,+34.8935+,+32.1722,+34.8935+,+32.1722,+34.8935+,+32.1723,+34.8935+,+32.1723,+34.8935+,+32.1727,+34.8935+,+32.1727,+34.8935+,+32.1728,+34.8935+,+32.1729,+34.8935+,+32.1729,+34.8935+,+32.1730,+34.8934+,+32.1732,+34.8933+,+32.1734,+34.8933+,+32.1734,+34.8922+,+32.1744,+34.8922+,+32.1744,+34.8913+,+32.1753,+34.8913+,+32.1753,+34.8906+,+32.1761,+34.8906+,+32.1761,+34.8903+,+32.1763,+34.8899+,+32.1769,+34.8896+,+32.1772,+34.8896+,+32.1772,+34.8891+,+32.1775,+34.8887+,+32.1778,+34.8886+,+32.1781,+34.8886+,+32.1781,+34.8891+,+32.1804,+34.8891+,+32.1804,+34.8893+,+32.1812,+34.8893+,+32.1812,+34.8900+,+32.1846,+34.8900+,+32.1846,+34.8902+,+32.1853,+34.8902+,+32.1856,+34.8902+,+32.1857,+34.8898+,+32.1886,+34.8898+,+32.1886,+34.8896+,+32.1896,+34.8896+,+32.1896,+34.8893+,+32.1912,+34.8893+,+32.1914,+34.8892+,+32.1916,+34.8892+,+32.1917,+34.8893+,+32.1919,+34.8893+,+32.1920,+34.8894+,+32.1920,+34.8895+,+32.1921,+34.8896+,+32.1922,+34.8897+,+32.1922,+34.8899+,+32.1922,+34.8900+,+32.1922,+34.8902+,+32.1922,+34.8903+,+32.1921,+34.8903+,+32.1920,+34.8904+,+32.1919,+34.8904+,+32.1918,+34.8905+,+32.1917,+34.8905+,+32.1916,+34.8904+,+32.1912,+34.8904+,+32.1912,+34.8898+,+32.1911,+34.8898+,+32.1911,+34.8891+,+32.1911,+34.8891+,+32.1911,+34.8885+,+32.1910,+34.8872+,+32.1912,+34.8872+,+32.1912,+34.8858+,+32.1913,+34.8858+,+32.1913,+34.8848+,+32.1914,+34.8844+,+32.1915,+34.8844+,+32.1915,+34.8838+,+32.1916,+34.8838+,+32.1916,+34.8820+,+32.1920,+34.8820+,+32.1920,+34.8811+,+32.1922,+34.8811+,+32.1922,+34.8810+,+32.1925,+34.8810+,+32.1928,+34.8810+,+32.1928,+34.8808+,+32.1934,+34.8808+,+32.1934,+34.8807+,+32.1940,+34.8807+,+32.1940,+34.8805+,+32.1953,+34.8805+,+32.1953,+34.8805+,+32.1958)",
how can I remove the + signs ?
In your getCoordsLists() method you are joining the collectors with ", "
thats why, after building the url it is adding spaces after every coordinate.
so just replace the ", " with "," in the following line:
String list = "( " + alternative.coords.stream().map(item -> String.format("%.4f , %.4f", item.x, item.y))
.collect(Collectors.joining(", ")) + ")";
It is happening because default url encoder is replacing the spaces by "+" so that the link will not be broken.
You should also replace the last line in the same way, where you are returning the string.

How can I split the data over multiple lines in a JTextArea?

How can I put each line of the host in the message body? I work with a JTextArea.
String host = InetAddress.getLocalHost().getHostName().toString();
texto_recepcion.setText(host + texto_recepcion.getText() + dpRecepcion.getAddress() + " " + mensaje_recibido + "\n");
How it is now:
I resolve my question with append function.
String host = InetAddress.getLocalHost().getHostName().toString();
texto_recepcion.append(host); // ***Use the function append for solve the problem***
texto_recepcion.setText(texto_recepcion.getText() + dpRecepcion.getAddress() + " " + mensaje_recibido + "\n");
Thanks a lot
Why don't you add the newline character "\n" to the beginning of the string?
texto_recepcion.setText("\n" + host + texto_recepcion.getText() + dpRecepcion.getAddress() + " " + mensaje_recibido);

How to pass dynamic value to a JSON String

I have constructed a JSON String this way , but cannot able to pass dynamic values to it
String input = "{\r\n" +
" \"Level\": 0,\r\n" +
" \"Name\": \"String\",\r\n" +
" \"msgName\": \"String\",\r\n" +
" \"ActualMessage\": \"String\",\r\n" +
" \"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
String message = "this is value want to pass to the ActualMessage attribute " ;
I need to pass dynamic value to the ActaulMessage atribute
Please tell me how ?
i have tried number of trial and errors but couldn't able to succeed.
Use string concatenation.
String message = "this is value want to pass to the ActualMessage attribute " ;
String input = "{\r\n" +
"\"Level\": 0,\r\n" +
"\"Name\": \"String\",\r\n" +
"\"msgName\": \"String\",\r\n" +
"\"ActualMessage\": \"" + message + "\",\r\n" +
"\"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
How about using String.format() for this? for example, to pass a "dynamic value" declare a place holder in the text:
String input = "insert %s in the string"; // here %s is the placeholder
input = String.format(input, "value"); // replace %s with actual value
Now input will contain the string "insert value in the string". In your example, change this line:
" \"msgName\": \"String\",\r\n"
Replace it with this:
" \"msgName\": \"%s\",\r\n"
Now you can perform the substitution:
input = String.format(input, message);
Notice that the first parameter in the format() method has a lot more of options, and that you can pass more than one argument to be replaced. Take a look at the documentation for the Formatter class.
if you want to manipulate Json please consider GSON. your problem can be addressed as follows.
String input = "{\r\n" +
" \"Level\": 0,\r\n" +
" \"Name\": \"String\",\r\n" +
" \"msgName\": \"MessageName\",\r\n" +
" \"ActualMessage\": \"%s\",\r\n" +
" \"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
String message = "this is value want to pass to the ActualMessage attribute " ;
String output=String.format(input,message);
//this will replace %s with the content of message variable.

Categories