문제

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?

도움이 되었습니까?

해결책

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

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top