1

I have a java string like this one :

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
scelerisque enim a ornare auctor. Duis quam nisi, mattis vel leo eu,
luctus porta <img src="http://www.test.com" ...

I would like to keep only this text in the previous string : (without <img ...)

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
scelerisque enim a ornare auctor. Duis quam nisi, mattis vel leo eu,
luctus porta

Could you help me to do this in Java?

wawanopoulos
  • 9,614
  • 31
  • 111
  • 166

4 Answers4

5

Assuming the only string you given and that too <img at the end.

String[] strArray = string.split("<img");
String result= strArray [0];
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

You could simply use .subString() and .indexOf():

result = string.subString(0, string.indexOf("<img"));

Though you'll need to check whether index is not -1 first...

fge
  • 119,121
  • 33
  • 254
  • 329
1
string.substring(0, string.indexOf("<img"));
bedrin
  • 4,458
  • 32
  • 53
0

A simple solution to delete tags would be

text = text.replaceAll("<[^>]*>","")

But I think it might need parsing as HTML and removing tags if there are more complicated tags with content inside.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105