Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 26th, 2006, 01:15 AM
Authorized User
 
Join Date: Oct 2004
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default String replace not functioning properly

Is there someone who have come across this situation.
Recently in one of my application i have to parse a CSV file and insert directly into the DB.
There was a lot null values in the CSV, so I had to replace it with "NULL" before inserting into the DB.
ie. if the CSV string contains the data
  "123,"XXXXX",,,1"
   My replacement would be
  "123,"XXXXX",NULL,NULL,1"

 But when using the replace functon

Code:
strCSVString.Replace(",,",",NULL,");
 the result was
 "123,"XXXXX",NULL,,1"

 It took quite a while for me to realise that I had to use the replace function twice to do this.
  ie.
  When I used
  strCSVString =
Code:
strCSVString.Replace(",,",",NULL,");

  strCSVString =
Code:
strCSVString.Replace(",,",",NULL,");


 it gave me the required output

 "123,"XXXXX",NULL,NULL,1"

 Is there something which I missing out here?
 OR is it a bug with C#.NET?

 I'm a regular customer to the replace funtion (When dealing with ASP,PHP,Coldfusion etc).. And when coming to C# it is really creating me a lot of problems...

 Please let me know if anyone can throw a light on this issue.


 
Old January 26th, 2006, 03:05 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi ghari,

I don't think the .NET Framework would have functioned world wide if it had such obvious bugs...

Anyway, I think this is what happening. You start with this:

"123,"XXXXX",,,1"

It has 3 consecutive commas. Now you call Replace like this:

strCSVString.Replace(",,",",NULL,");

You replace two commas with NULL, In you original string, there is only one occurence of two consecutive commas. So, after the first call to Replace, you end up with:

"123,"XXXXX",NULL,,1"

This result is then stored back in strCSVString.

As you can see, there was no more than 1 occurence of the double commas up front, so the Replace function could only replace one. After Replace was called, you had another, but that was because you inserted a new one using NULL,

It's still a bit early in the morning here, so I might not be making sense, but I hope you understand what I am saying....

To fix this, use a loop to replace stuff while there are still two commas in strCSVString.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 26th, 2006, 10:41 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

A better approach for this might be to let the program do the hard work:

Take your inbound value and split it by the delimiter into an array:

string[] strValues = strInbound.Split(',');
//Note: using ' to enclose the delimiter implies the char type in C#
//Use ","c in VB.NET

Then you can spin thru the array and replace all occurences of String.Empty ("") with "NULL". Then you also have a data structure from which to construct your database query instead of just another string.

-Peter
 
Old January 27th, 2006, 12:16 AM
Authorized User
 
Join Date: Oct 2004
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar, I think you are right.
I just tried with ASP and ended up with the same result.
May be It was the first time I was using such a trick on this particular type of string. :)

Thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple String Replace berna_isct XSLT 5 May 20th, 2008 02:47 AM
Help in instruction string-replace hugoscp XML 9 October 20th, 2006 12:07 PM
replace string value str33ts XSLT 1 April 22nd, 2005 04:01 AM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM
String.replace() method JohnD Javascript 0 August 20th, 2004 06:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.