IndexOf fails to count correctly on first run. Why?

Hello

I have a string I extract New/Old info from. Looks like this below.
009CMNDSEQ665423412004OLDOFF IBA1004NEWOFF IS4S001PRO

Based on the Int before OLD/NEW, I strip the spaces, indexOf and fetch the Int number of chars 6 places after either OLD/NEW.

I fetch NEW first and in this example it incorrectly picks up S4S0, then it fetches OLD and correctly picks up IBA1.

NumOfChar is always correct for both. Why and how is IndexOf incorrectly counting the first # of chars ??

Code used
NumOfChar
in_TransactionItem.SpecificContent(“CommandData”).ToString.Substring(in_TransactionItem.SpecificContent(“CommandData”).ToString.IndexOf(“NEW”)-1, 1)

NewOff
in_TransactionItem.SpecificContent(“CommandData”).ToString.Replace(" ",String.Empty).Substring(in_TransactionItem.SpecificContent(“CommandData”).ToString.IndexOf(“NEW”)+6, Cint(NumOfChar))

NumOfChar
in_TransactionItem.SpecificContent(“CommandData”).ToString.Substring(in_TransactionItem.SpecificContent(“CommandData”).ToString.IndexOf(“OLD”)-1, 1)

OldOff
in_TransactionItem.SpecificContent(“CommandData”).ToString.Replace(" ",String.Empty).Substring(in_TransactionItem.SpecificContent(“CommandData”).ToString.IndexOf(“OLD”)+6, Cint(NumOfChar))

Hi @Futan

Try this:

newNumOfChar = CInt(in_TransactionItem.SpecificContent("CommandData").ToString.Substring(in_TransactionItem.SpecificContent("CommandData").ToString.IndexOf("NEW") + 3, 1))


newOff = in_TransactionItem.SpecificContent("CommandData").ToString.Replace(" ", String.Empty).Substring(in_TransactionItem.SpecificContent("CommandData").ToString.IndexOf("NEW") + 4, newNumOfChar)


oldNumOfChar = CInt(in_TransactionItem.SpecificContent("CommandData").ToString.Substring(in_TransactionItem.SpecificContent("CommandData").ToString.IndexOf("OLD") + 3, 1))


oldOff = in_TransactionItem.SpecificContent("CommandData").ToString.Replace(" ", String.Empty).Substring(in_TransactionItem.SpecificContent("CommandData").ToString.IndexOf("OLD") + 4, oldNumOfChar)

hey @Futan

commandData = in_TransactionItem.SpecificContent(“CommandData”).ToString()
commandDataNoSpaces = commandData.Replace(" ", String.Empty)

numOfCharNew = CInt(commandDataNoSpaces.Substring(commandDataNoSpaces.IndexOf(“NEW”)-1, 1))

numOfCharOld = CInt(commandDataNoSpaces.Substring(commandDataNoSpaces.IndexOf(“OLD”)-1, 1))

newOff = commandDataNoSpaces.Substring(commandDataNoSpaces.IndexOf(“NEW”)+3, numOfCharNew)

oldOff = commandDataNoSpaces.Substring(commandDataNoSpaces.IndexOf(“OLD”)+3, numOfCharOld)

Hi @Futan

Try the below syntaxes:

NumOfChar = in_TransactionItem.SpecificContent("CommandData").ToString.Substring(in_TransactionItem.SpecificContent("CommandData").ToString.IndexOf("NEW") - 1, 1)

NewOff = in_TransactionItem.SpecificContent("CommandData").ToString.Replace(" ", String.Empty).Substring(in_TransactionItem.SpecificContent("CommandData").ToString.IndexOf("NEW") + 5, CInt(NumOfChar))

NumOfChar = in_TransactionItem.SpecificContent("CommandData").ToString.Substring(in_TransactionItem.SpecificContent("CommandData").ToString.IndexOf("OLD") - 1, 1)

OldOff = in_TransactionItem.SpecificContent("CommandData").ToString.Replace(" ", String.Empty).Substring(in_TransactionItem.SpecificContent("CommandData").ToString.IndexOf("OLD") + 5, CInt(NumOfChar))

Hope it helps!!

I should have expanded on my initial message. This code was tested and working 27Nov. When pushing to live 27Jan, it’s incorrectly picking the NEWOFF. The strings I’m looking for are NEWOFF and OLDOFF, hence the +6. I wrote it like that so I could use the same for NEWTTC and OLDTTC etc.

If I have a command sting that only contains NEWOFF and I use the first 2 items in my image, then it works perfectly. The minute it also has to go find OLDOFF in the same block, it fails the count.

So thanks for the replies.

Supriya - with a slight change to the numbers to include OFF, the result was still incorrect.
Parvathy - with that change to the index count, NEWOFF works, but OLDOFF fails.

Piotr - this is useful as a workaround, I did something similar in doing more assigns in a test, my problem then is I have to then add to and update 11 switches. I’m still stumped as to why it works if it’s only run with looking for NEWOFF, but breaks while looking for both.

Hi @Futan ,

I believe the Expression for the NewOff and Old Off should be changed to the below :

New Off :

Info.Substring(Info.IndexOf("NEW")+6).Replace(" ","").Substring(0,CInt(NumOfChar))

Old Off :

Info.Substring(Info.IndexOf("OLD")+6).Replace(" ","").Substring(0,CInt(NumOfChar))

Debug Panel :
image

It is because of the Spaces the error happens, the OLDOFF Replace Empty String removes only one space, where as the NEWOFF replaces two spaces and then computes the Substring, which gives it incorrect value.

The above simply handles this By first Capturing the data from the NEWOFF and OLDOFF, then replacing spaces with empty string, making sure that whatever spaces after it is removed and then Performing the required Substring with Num of Character extracted.

We are not sure of the data present then, maybe the data did not have spaces ?

@Futan

You can try using regex

For Character count - ``(?<=NEW[a-zA-Z]{3} ?)\S{4}`

For value - \d{1}(?=NEW[a-zA-Z]{3} ?)

Also what @supermanPunch said makes sense about spaces…to avoid all thsoe this would be a better approach as well

Similar statement for old

(?<=OLD[a-zA-Z]{3} ?)\S{4}

Usage:
Count = System.Text.RegularExpressions.Regex.Match(YourStringVariable,"\d{1}(?=NEW[a-zA-Z]{3} ?)").Value

Value = System.Text.RegularExpressions.Regex.Match(YourStringVariable,"(?<=NEW[a-zA-Z]{3} ?)\S{" + count + "}").Value

4 is replaced with count to make it dynamic…this works even if there is a space or no space

Cheers

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.