PC Review


Reply
Thread Tools Rate Thread

C# String Trim() Error!

 
 
Dat K. AU DUONG
Guest
Posts: n/a
 
      15th Jun 2006
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.
 
Reply With Quote
 
 
 
 
hiteshmangal@gmail.com
Guest
Posts: n/a
 
      15th Jun 2006
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


Dat K. AU DUONG wrote:
> 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.


 
Reply With Quote
 
Dat K. AU DUONG
Guest
Posts: n/a
 
      15th Jun 2006
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.

(E-Mail Removed) wrote:
> 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
>
>
> Dat K. AU DUONG wrote:
>> 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.

>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Jun 2006
Dat K. AU DUONG <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Dat K. AU DUONG
Guest
Posts: n/a
 
      15th Jun 2006
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.

Jon Skeet [C# MVP] wrote:
> Dat K. AU DUONG <(E-Mail Removed)> wrote:
>> 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.
>

 
Reply With Quote
 
hiteshmangal@gmail.com
Guest
Posts: n/a
 
      15th Jun 2006
Wecome Dat

Thanks,
Hitesh
Dat K. AU DUONG wrote:

> 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.
>
> Jon Skeet [C# MVP] wrote:
> > Dat K. AU DUONG <(E-Mail Removed)> wrote:
> >> 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.
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to trim .key off the end of string laurasaur Microsoft C# .NET 5 3rd Apr 2007 07:23 AM
trim(string) vs string.trim Terry Olsen Microsoft VB .NET 22 26th Aug 2006 06:34 PM
String.Trim BK Microsoft VB .NET 6 4th Apr 2006 09:14 PM
Trim String Gale Coleman Microsoft Access 7 11th Nov 2004 04:49 AM
Trim a string??? Niklas Östergren Microsoft Access 4 8th Apr 2004 02:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:31 PM.