gpt4 book ai didi

java - 如何让 AttributedCharacterIterator 返回给定属性的运行?

转载 作者:搜寻专家 更新时间:2023-11-01 03:55:53 25 4
gpt4 key购买 nike

假设您将自定义 CharacterIterator.Attribute 分配给十个字符的字符串的前五个字符。

进一步假设您为剩余的字符分配一个不同的 CharacterIterator.Attribute

那么,为什么当我调用 AttributedString.getRunStart(firstAttribute) 时我得到 0(我希望如此),而当我调用 AttributedString.getRunStart(secondAttribute) 时我得到 0 得到0?

这是我的设置代码:

final AttributedString s = new AttributedString("SQ3R9FFFFF");
final Attribute baseID = new Attribute("Base ID") {};
final Attribute fs = new Attribute("FFF") {};
s.addAttribute(baseID, "Ignored", 0, 5);
s.addAttribute(fs, "Whatever", 5, 10);
final AttributedCharacterIterator iterator = s.getIterator();
assertNotNull(iterator);

现在这里有一些输出一些诊断信息的代码:

for (char c = iterator.first(); c != DONE; c = iterator.next()) {
System.out.println("Character: " + c);
System.out.println("Character index: " + iterator.getIndex());
System.out.println("Attributes: " + iterator.getAttributes());
System.out.println("Start for baseID: " + iterator.getRunStart(baseID));
System.out.println("Limit for baseID: " + iterator.getRunLimit(baseID));
System.out.println("Start for fs: " + iterator.getRunStart(fs));
System.out.println("Limit for fs: " + iterator.getRunLimit(fs));
}

输出是这样的:

 Character: S
Character index: 0
Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
Start for baseID: 0
Limit for baseID: 5
Start for fs: 0
Limit for fs: 5
Character: Q
Character index: 1
Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
Start for baseID: 0
Limit for baseID: 5
Start for fs: 0
Limit for fs: 5
Character: 3
Character index: 2
Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
Start for baseID: 0
Limit for baseID: 5
Start for fs: 0
Limit for fs: 5
Character: R
Character index: 3
Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
Start for baseID: 0
Limit for baseID: 5
Start for fs: 0
Limit for fs: 5
Character: 9
Character index: 4
Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
Start for baseID: 0
Limit for baseID: 5
Start for fs: 0
Limit for fs: 5
Character: F
Character index: 5
Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
Start for baseID: 5
Limit for baseID: 10
Start for fs: 5
Limit for fs: 10
Character: F
Character index: 6
Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
Start for baseID: 5
Limit for baseID: 10
Start for fs: 5
Limit for fs: 10
Character: F
Character index: 7
Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
Start for baseID: 5
Limit for baseID: 10
Start for fs: 5
Limit for fs: 10
Character: F
Character index: 8
Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
Start for baseID: 5
Limit for baseID: 10
Start for fs: 5
Limit for fs: 10
Character: F
Character index: 9
Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
Start for baseID: 5
Limit for baseID: 10
Start for fs: 5
Limit for fs: 10

请特别注意最后一个条目,它报告“baseID”的“Start”是 5。嗯?

最佳答案

根据 Javadoc:

A run with respect to an attribute is a maximum text range for which:

  • the attribute is undefined or null for the entire range, or
  • the attribute value is defined and has the same non-null value for the entire range.

我认为这可能是第一个要点。对于字符 0-4,fs 属性未定义,因此它是一个有效范围。 baseID 已定义,也有效。

http://download.oracle.com/javase/1,5.0/docs/api/java/text/AttributedCharacterIterator.html

关于java - 如何让 AttributedCharacterIterator 返回给定属性的运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5957649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com