Question

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?

Était-ce utile?

La solution

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

String[] strArray = string.split("<img");
String result= strArray [0];

Autres conseils

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...

string.substring(0, string.indexOf("<img"));

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top