jerome
jerome

Reputation: 13

Conversion from VB6 to C# string manipulation

VB6 code is as follows:

record = Collection & Right(TableName, Len(TableName) - (InStr(1, TableName, "_<idNo>_") + 7))

I've tried keeping the logic when changing it to c# but doesnt seem to work.

collection = 111111   
Record = collection + tablename.Substring(tablename.Length -  tablename.Length - tablename.IndexOf("_<idNo>_", 1) + 7);

(VB6)InStr is (C#)indexOf
See: http://bytes.com/topic/net/answers/108174-c-equilivant-instr

(VB6)Right is (C#)Substring and I'm following the template of how they change one to another. See: http://social.msdn.microsoft.com/Forums/vstudio/en-US/9598905f-912f-4ea7-b954-eb2f48328ce5/c-equivalent-for-right-of-vb

Expecting: 111111fiddlein

Getting: 111111o>_fiddlein

Also, when editing the + 7 at the end it doesn't seem to eliminate the underscore between the concatenation.
But instead I get: 111111o>_fiddlein

Upvotes: 0

Views: 122

Answers (2)

jac
jac

Reputation: 9726

Your problem is VB6 InStr function is 1 based, and C# IndexOf function is 0 based.

Upvotes: 2

Andy
Andy

Reputation: 4097

I assume the following:

string collection = "111111";
string tablename = "t_<idNo>_fiddlein"; // anything before '<idNo>_' will not be observed

Then this should do it:

string result = collection + tablename.Substring(tablename.IndexOf("_<idNo>_") + 8);

Upvotes: 3

Related Questions