Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5 string
Hi i all having a requirement to delete particular data from a text file. I have written some code to get my exact data to remove but now i am little bit confused in removing that particular data can any one help me.
 
Initially this is my data in text file where i am storing to a Stringbuilder
 
101 111111111 1111111111110150105A094101                                                      
 
52201               1                   1         PPD1         111015111015   1111000020000001
 
6241110000251                00000000011              1                     1 0111000020000001
 
822000000100111000020000000000000000000000011                                  111000020000001
 
52251               1                   1         PPD1         011015111015   1111000020000002
 
6281110000251                00000000011              1                     1 0111000020000002
 
822500000100111000020000000000010000000000001                                  111000020000002
 
9000002000001000000020022200004000000000001000000000001                                       
 
Now by using certain code i am getting the data to delete i.e assume i would like to delete from 5 to 8 which exists. I am storing this to another StringBuilder as follows
 
52251               1                   1         PPD1         011015111015   1111000020000002
 
6281110000251                00000000011              1                     1 0111000020000002
 
822500000100111000020000000000010000000000001                                  111000020000002
 
I would like to remove this from the main Stringbuilder i am having. Is this the correct process or if any better way let me know
Posted 14-Oct-11 23:35pm
Comments
DaveAuld at 15-Oct-11 4:43am
   
What code are you using? is the text file data line terminated or a single long string? It might be better to work with an array of line entries if it is line terminated.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You could try using the content of the second StringBuilder to remove it from the first one by using the Replace[^] method.
 
It would look something like:
mainStringBuilder.Replace(anotherStringBuilder.ToString(), "");
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Yes - deleting from a StringBuilder should be a lot more efficient that creating a new one. If you know the first character index and the length (which you can obviously work out from the total length and the character index) then you can just use the StringBuilder.Remove method[^]
  Permalink  
Comments
Dorababu743 at 15-Oct-11 4:50am
   
But in my file there is no limit for that. I can have n no of lines from 5 to 8
OriginalGriff at 15-Oct-11 4:57am
   
As long as you know the index of the first character you want to delete (and if you are line based, then you presumably keep a list of line start indices) than deleting to the end is simple.
Or am I misinterpreting your problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 15 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100