0

I am reading the iptables log output directly from the console and I need to clear the results a little bit.

Currently I am getting:

SRC=192.168.1.1 PROTO=UDP DPT=12638
SRC=192.168.1.1 PROTO=UDP DPT=5636
SRC=192.168.1.1 PROTO=UDP DPT=8861
SRC=192.168.1.1 PROTO=UDP DPT=15114

I am trying to remove SRC= , PROTO= and DPT=...

String results=a.toString();

results.replace("SRC="," ");
results.replace("PROTO="," ");
results.replace("DF TYPE=3"," ");

tv=(TextView)findViewById(R.id.tv1);
tv.setText("results:\n"+results);

And of course nothing is removed :D

David
  • 15,894
  • 22
  • 55
  • 66
Philip St
  • 81
  • 10

3 Answers3

1

use regex this way

results=results.replaceAll("(SRC|PROTO|DF TYPE)=", " ");

This will replace src or proto or df type with a " "(space) in a single line.No need of writing replace 3 times

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
-1

try this way:

results = results.replaceAll("SRC="," ");
results = results.replaceAll("PROTO="," ");

and @Sotirios Delimanolis is right.

guness
  • 6,336
  • 7
  • 59
  • 88
-1

Change this:

results.replace("SRC="," ");
results.replace("PROTO="," ");
results.replace("DF TYPE=3"," ");

To this:

results = results.replace("SRC="," ");
results = results.replace("PROTO="," ");
results = results.replace("DF TYPE=3"," ");

But you said you wanted to get rid of them, so I would guess that you don't want a blank space, I would guess you want nothing there. You also said "I am trying to remove SRC= , PROTO= and DPT=...", so why do you do this: replace("DF TYPE=3","")? I suggest this:

results = results.replace("SRC=","");
results = results.replace("PROTO=","");
results = results.replace("DPT=","");

Or this (credit to Jason Sperske):

results = results.replaceAll("(SRC|PROTO|DPT)=", " ");

.replace(String, String) will return the replaced String, but will not change the value of the current existing String. As Sotirios Delimanolis said, Strings are immutable.

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97