为什么不的IndexOf工作与文字2字节? [英] Why IndexOf not working with text 2 byte?
问题描述
我有这个code为( parm_strLineData code>是类型的参数
字符串
):
parm_strLineData =<文字>:< AK_F NO =1> 7< / AK_F取代。ポジ< /文字>
昏暗iPosTagClose_Start作为整数= parm_strLineData.IndexOf(< / AK,0)
结果: iPosTagClose_Start = -1
注:人物:为2字节
为什么不的IndexOf工作与文字2字节?
如何解决?
昏暗parm_strLineData作为字符串
parm_strLineData =<文字>:< AK_F NO =1> 7< / AK_F> ???< /文字>
下面创建一个的XElement
和隐含其转换为字符串。
其结果是, parm_strLineData code>现在这样的没有的包含的字符串的
<文本和GT ;:< AK_F NO =1> 7< / AK_F> ???< /文字>
,而所有元素的文字和后代的文本的连接字符串值。
在你的情况,这是:7゜ポジ
和:7゜ポジ
不包含< / AK
,这样的结果的IndexOf
是 1
。
我不知道你的真正的问题,但要解决这个问题,使用字符串
代替的XElement
:
parm_strLineData的String =<文字>:< AK_F NO =1> 7< / AK_F> ???< /文本和GT;
如果你的目标是要检查XML节点的存在,不使用字符串分析,但如Linq2Xml。
I have this code as (parm_strLineData
is a parameter of type String
):
parm_strLineData = <Text>:<AK_F NO="1">7</AK_F>。ポジ</Text>
Dim iPosTagClose_Start As Integer = parm_strLineData.IndexOf("</AK",0)
Result: iPosTagClose_Start = -1
Note: character: 。 is 2byte
Why IndexOf not working with text 2 byte?
How fix?
Dim parm_strLineData As String
parm_strLineData = <Text>:<AK_F NO="1">7</AK_F>???</Text>
Here you create a XElement
and implicitly convert it to a string.
The result is that parm_strLineData
now does not contain the string <Text>:<AK_F NO="1">7</AK_F>???</Text>
, but the concatenated string value of all of the element's text and descendant's text.
In your case, it's :7゜ポジ
, and :7゜ポジ
does not contain "</AK"
, so the result of IndexOf
is -1
.
I don't know your real problem, but to solve this issue, use a string
instead of an XElement
:
parm_strLineData As String= "<Text>:<AK_F NO=""1"">7</AK_F>???</Text>"
If your goal is to check if an XML node exists, don't use string parsing, but e.g. Linq2Xml.
这篇关于为什么不的IndexOf工作与文字2字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!