[英]check if string found in String in java android studio
description = "<p>[lang=\"en\"]The large ones[/lang]</p>"
if (description.startsWith("<p>")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
description = Html.fromHtml(description, Html.FROM_HTML_MODE_COMPACT).toString();
} else {
description=Html.fromHtml(description).toString();
}
}
//this condition not work string dosn't change //这个条件不起作用字符串不会改变
if (description.matches("\\[lang=\\\"en\\\"\\]")) {
int lenght = description.length();
description.substring(7,lenght-7);
}
i need to remove all html and header [lang=\"en"\]
from string and the output is:我需要从字符串中删除所有 html 和 header
[lang=\"en"\]
并且 output 是:
" [lang="en"]The large ones[/lang] "
" [lang="en"]大的[/lang] "
After applying申请后
String subString = description.substring(7,lenght-7);
Just use replace.只需使用替换。
subString = subString.replace("[lang=\"en\"]" , "");
subString = subString.replace("[/lang]" , "");
I will break the resulting path into two steps -我会将生成的路径分为两个步骤 -
Step 1: Removing all HTML tags步骤 1:删除所有 HTML 标签
String description = "<p>[lang=\"en\"]The large ones[/lang]</p>"
String stringTemp = Html.fromHtml(description).toString();
This will give [lang=\"en\"]The large ones[/lang]
no matter how many HTML tag are present.无论有多少 HTML 标记存在,这都会给出
[lang=\"en\"]The large ones[/lang]
。 Eg.例如。 result will be same for all these values of description.
对于所有这些描述值,结果将相同。
> <i><b><p>[lang=\"en\"]The large ones[/lang]</p></b></i>
> <HTML><b><p>[lang=\"en\"]The large ones[/lang]</p></b></HTML>
> <HTML><HEAD><b><p>[lang=\"en\"]The large ones[/lang]</p></b></HEAD></HTML>
Step 2: Get StringValue between [lang="en"]StringValue[/lang]第 2 步:获取 [lang="en"]StringValue[/lang] 之间的 StringValue
Once we have our desired String we can manipulate if using many way like using String.subString()
or use String.replace()
suggested by @Khaled Alramam.一旦我们有了我们想要的字符串,我们就可以使用多种方式进行操作,例如使用
String.subString()
或使用 @Khaled Alramam 建议的String.replace()
。
Using subString()
as follows -使用
subString()
如下 -
/*
* String.subString(beginIndex, endIndex);
* Note: beginIndex is inclusive but endIndex is the exclusive
* */
String realString = stringTemp.substring(11,(stringTemp.length()-9));
Now realString
will have result as The large ones
or any string value so far.现在
realString
的结果将是The large ones
或到目前为止的任何字符串值。
Happy coding !快乐编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.