删除一些字符后如何调整字符串中的索引? [英] How to adjust index in a string after removing some characters?
问题描述
我对正则表达式和文本操作很陌生,所以这对很多人来说可能是微不足道的,但对我来说却是一件很麻烦的事情.
I am very new to regex and text manipulation so this might be trivial for many but it is turning out to be quite a nuisance for me.
我有一个像这样的字符串:
I have a string like so:
*text* text text text text *text*
*text* text text text *text*
其中带 * 的黑白文本表示必须为粗体的文本
所以期望的输出是
where the text b/w the * denotes the text that has to be bold
So the desired output is
text text text text text
text text text text text
我已经使用以下代码完成了一半
I am halfway there using the following code
val string = "*text* text text text *text*"
val pattern = Pattern.compile("\\*")
val matcher = pattern.matcher(string)
val spannableString = SpannableString(string)
var counter = 0
val indexes = IntArray(1)
while (matcher.find()) {
if(counter==0){
indexes[0]=matcher.start()
counter+=1
}else if(counter==1){
spannableString.setSpan(
StyleSpan(Typeface.BOLD),
indexes[0],matcher.start(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
counter+=1
}
if(counter==2){
counter=0
}
}
textView.text = spannableString
}
现在的输出是
*text* text text text *text*(* 也是粗体)
我想删除 * 的
我打算使用
The output right now is
*text* text text text *text* (the *s are bold as well)
I want to remove the *'s
I intend to use
val spannableString = SpannableString(string.replace("*",""))
删除*,但在那之后,我无法调整需要传递的索引.我该怎么做?或者,还有更好的方法?非常感谢任何帮助
to remove the *'s but after that, I am not able to adjust the indexes that need to be passed on. How do I do it? or is there a better way? Any help is greatly appreciated
推荐答案
IMO 你真的不需要正则表达式来解决你的问题(有句名言 如果你想用正则表达式解决问题,现在你有两个问题",我经常使用正则表达式来表示同意和不同意).
IMO you don't really need a regex to solve your problem (There's a famous saying "If you're trying to solve a problem with RegEx, now you have 2 problems", and I use RegExs frequently enough to both agree and disagree).
您可以在循环中的 String
中使用 indexOf
方法 (Java, Kotlin) 找到 *
s,同时在 SpannableStringBuilder:
You can use the indexOf
method in String
in a loop (Java, Kotlin) to find the *
s, while constructing your end result in a SpannableStringBuilder:
- 保持一个变量指向当前跨度的开头(如果没有打开的跨度,则为 -1).
- 在每次迭代中,您都使用
indexOf
从找到的最后一个索引开始. - 每次找到开头的
*
时,您都会将最后一个位置到当前位置的文本附加到您的构建器中. - 每次找到结束的
*
时,都将开头*
的文本附加到接近的文本,并将其设置为粗体. - 如果在迭代中没有找到
*
,则将最后一站的所有内容附加到构建器 - 如果您到达字符串的末尾并且有一个从未关闭的打开的
*
,那么您就知道发生了错误.您可以应用或不应用粗体跨度(或throw
).
- Keep a variable pointing to the start of the current span (or -1 if no open spans).
- In every iteration, you use
indexOf
starting from the last index found. - Every time you find an opening
*
you append the text from the last position to the current one to your builder. - Every time you find a closing
*
you append the text from the opening*
to the closer, and set it as a bold span. - If you don't find a
*
at an iteration you append everything from the last stop to the builder - If you reach the end of the string and have an open
*
that was never closed you know you have an error. You can either apply a bold span or not (orthrow
).
希望这会有所帮助.
另外,请不要用指向您的链接的其他问题劫持.
Also, please don't hijack other questions with links to yours.
这篇关于删除一些字符后如何调整字符串中的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!