D T
D T

Reputation: 3746

Why IndexOf not working with text 2 byte?

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?

Upvotes: 1

Views: 141

Answers (1)

sloth
sloth

Reputation: 101032

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.

Upvotes: 2

Related Questions