C# String Trim() Error!

D

Dat K. AU DUONG

Hi,

C# Annoying string function or Am I doing Something wrong?

In VB6/VB.NET I can code like:
value = value.trim()

and it just work!


But in c#, I event put in the additional check I still get the error.
if (value.Length > 0 && value != string.Empty){
value = value.trim();
}

The error message:
startIndex cannot be larger than length of string.
Parameter name: startIndex at
System.String.InternalSubStringWithChecks(Int32 startIndex, Int32
length, Boolean fAlwaysCopy) ...

Please Help!
Thanks.
Regards Dat.
 
H

hiteshmangal

Hi Dat,


This code is working fine on my end and I am getting the expected
output.Could u please more elobrate ur problem.

Thanks,
Hitesh
 
D

Dat K. AU DUONG

Hi Hitesh,

My code extract data from the mshtml.HTMLElement

string value;
mshtml.IHTMLDocument2 doc;
mshtml.IHTMLElementCollection tbls = (mshtml.IHTMLElementCollection)
((mshtml.IHTMLElementCollection) doc.body.all).tags("table");
mshtml.HTMLTable tbl = (mshtml.HTMLTable) tbls.item(1,1);
mshtml.HTMLTableRow tblRow = (mshtml.HTMLTableRow) tbl.rows.item(1,1);

if (tblRow.cells != null)
{
mshtml.IHTMLElement tblElement = (mshtml.IHTMLElement)
tblRow.Cells.item(2,2);

value = tblElement.innerText;

// Error Occur Here!
if (value.Length > 0 && value != string.Empty){
value = value.Trim();
}
}

I think the problem occur when the innerText is Empty and I am trying to
trim the value.

Regards Dat.
 
J

Jon Skeet [C# MVP]

Dat K. AU DUONG said:
Hi Hitesh,

My code extract data from the mshtml.HTMLElement

string value;
mshtml.IHTMLDocument2 doc;
mshtml.IHTMLElementCollection tbls = (mshtml.IHTMLElementCollection)
((mshtml.IHTMLElementCollection) doc.body.all).tags("table");
mshtml.HTMLTable tbl = (mshtml.HTMLTable) tbls.item(1,1);
mshtml.HTMLTableRow tblRow = (mshtml.HTMLTableRow) tbl.rows.item(1,1);

if (tblRow.cells != null)
{
mshtml.IHTMLElement tblElement = (mshtml.IHTMLElement)
tblRow.Cells.item(2,2);

value = tblElement.innerText;

// Error Occur Here!
if (value.Length > 0 && value != string.Empty){
value = value.Trim();
}
}

I think the problem occur when the innerText is Empty and I am trying to
trim the value.

I don't think the problem is where you think it is.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
D

Dat K. AU DUONG

Dear Jon and Hitesh

Thank your!

I have found the problem, it was due to the tblElement that I am
refering to does not exist.

It never occurr to me to think twice whether it could be something else.
Because when I don't put the Trim() statement in it work! (Without
Error/Warning).

That's why I thought it was having to do with the Trim() function.

Thanks again for your time!
Regards Dat.
 
H

hiteshmangal

Wecome Dat

Thanks,
Hitesh said:
Dear Jon and Hitesh

Thank your!

I have found the problem, it was due to the tblElement that I am
refering to does not exist.

It never occurr to me to think twice whether it could be something else.
Because when I don't put the Trim() statement in it work! (Without
Error/Warning).

That's why I thought it was having to do with the Trim() function.

Thanks again for your time!
Regards Dat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top