質問

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