473,698 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.I nternalSubStrin gWithChecks(Int 32 startIndex, Int32
length, Boolean fAlwaysCopy) ...

Please Help!
Thanks.
Regards Dat.
Jun 15 '06 #1
5 15007
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.I nternalSubStrin gWithChecks(Int 32 startIndex, Int32
length, Boolean fAlwaysCopy) ...

Please Help!
Thanks.
Regards Dat.


Jun 15 '06 #2
Hi Hitesh,

My code extract data from the mshtml.HTMLElem ent

string value;
mshtml.IHTMLDoc ument2 doc;
mshtml.IHTMLEle mentCollection tbls = (mshtml.IHTMLEl ementCollection )
((mshtml.IHTMLE lementCollectio n) doc.body.all).t ags("table");
mshtml.HTMLTabl e tbl = (mshtml.HTMLTab le) tbls.item(1,1);
mshtml.HTMLTabl eRow tblRow = (mshtml.HTMLTab leRow) tbl.rows.item(1 ,1);

if (tblRow.cells != null)
{
mshtml.IHTMLEle ment tblElement = (mshtml.IHTMLEl ement)
tblRow.Cells.it em(2,2);

value = tblElement.inne rText;

// 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**********@gm ail.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.I nternalSubStrin gWithChecks(Int 32 startIndex, Int32
length, Boolean fAlwaysCopy) ...

Please Help!
Thanks.
Regards Dat.

Jun 15 '06 #3
Dat K. AU DUONG <da*********@sp rusons.com.au> wrote:
Hi Hitesh,

My code extract data from the mshtml.HTMLElem ent

string value;
mshtml.IHTMLDoc ument2 doc;
mshtml.IHTMLEle mentCollection tbls = (mshtml.IHTMLEl ementCollection )
((mshtml.IHTMLE lementCollectio n) doc.body.all).t ags("table");
mshtml.HTMLTabl e tbl = (mshtml.HTMLTab le) tbls.item(1,1);
mshtml.HTMLTabl eRow tblRow = (mshtml.HTMLTab leRow) tbl.rows.item(1 ,1);

if (tblRow.cells != null)
{
mshtml.IHTMLEle ment tblElement = (mshtml.IHTMLEl ement)
tblRow.Cells.it em(2,2);

value = tblElement.inne rText;

// 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.co m>
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*********@sp rusons.com.au> wrote:
Hi Hitesh,

My code extract data from the mshtml.HTMLElem ent

string value;
mshtml.IHTMLDoc ument2 doc;
mshtml.IHTMLEle mentCollection tbls = (mshtml.IHTMLEl ementCollection )
((mshtml.IHTMLE lementCollectio n) doc.body.all).t ags("table");
mshtml.HTMLTabl e tbl = (mshtml.HTMLTab le) tbls.item(1,1);
mshtml.HTMLTabl eRow tblRow = (mshtml.HTMLTab leRow) tbl.rows.item(1 ,1);

if (tblRow.cells != null)
{
mshtml.IHTMLEle ment tblElement = (mshtml.IHTMLEl ement)
tblRow.Cells.it em(2,2);

value = tblElement.inne rText;

// 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*********@sp rusons.com.au> wrote:
Hi Hitesh,

My code extract data from the mshtml.HTMLElem ent

string value;
mshtml.IHTMLDoc ument2 doc;
mshtml.IHTMLEle mentCollection tbls = (mshtml.IHTMLEl ementCollection )
((mshtml.IHTMLE lementCollectio n) doc.body.all).t ags("table");
mshtml.HTMLTabl e tbl = (mshtml.HTMLTab le) tbls.item(1,1);
mshtml.HTMLTabl eRow tblRow = (mshtml.HTMLTab leRow) tbl.rows.item(1 ,1);

if (tblRow.cells != null)
{
mshtml.IHTMLEle ment tblElement = (mshtml.IHTMLEl ement)
tblRow.Cells.it em(2,2);

value = tblElement.inne rText;

// 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
11642
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 1.3.1 I've tried: String CUNM = request.getParameter("CUNM").trim(); CUNM = URLDecoder.decode(CUNM,"UTF-8");
11
5347
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 (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
2
2235
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 values. My parameters in the database are like this @DealerShipID int, @ReturnDate smallDateTime,
10
16413
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 recognized as a valid Boolean. Public Sub UpdateCustomer_DashboardGraphs(ByVal sender As Object, ByVal e As System.EventArgs)
1
6056
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 during the runtime, the user can click on a button and change the connection string without exiting the application. I would really appreciate any sort of help.
0
1552
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 databases in that server. Then after the user selects a database, I have another button that he/she click and I want to retrieve file groups from a specific table. At this point when he/she clicks on that button I get an error:
8
2835
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 to modify s. s.Trim('\r'); While the follow code DOES modify s.
8
3251
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 else replying to Kevin Smith's article, please *ignore* the Followup-To header and post only to the csharp group. Thanks.
24
3091
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 Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.