473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C# String Trim() Error!

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.
Jun 15 '06 #1
5 14993
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.


Jun 15 '06 #2
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.

hi**********@gmail.com 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.

Jun 15 '06 #3
Dat K. AU DUONG <da*********@sprusons.com.au> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 15 '06 #4
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 <da*********@sprusons.com.au> 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.

Jun 15 '06 #5
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 <da*********@sprusons.com.au> 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.


Jun 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: knocker | last post by:
Hi I have a problem with JSP on websphere 5. When I try save information with swedish or danish ÅÄÖ characters, the string is cut where the first of these characters occurs. The JDK used is...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
2
by: Learner | last post by:
Hello, I am trying to store the data entered in a webform in the database. I have few Int and one SmallDateTime filed in my table in SQL Server 2005 database. I have made a storedproc to store the...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
8
by: Kevin Smith | last post by:
Hi, According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear...
8
by: Keith Thompson | last post by:
Kevin Smith <no@spam.comwrites: You posted this to microsoft.public.dotnet.languages.csharp, where I presume it's topical. Why on Earth did you redirect followups to comp.lang.c? Anyone...
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.