Click here to Skip to main content
14,927,434 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi!

From back end I'm fetching 250 records of contractors and storing in each separate string builder using string builder I'm append the data to the array list from these generating the file format.

Here I'm struggling with some problem that is unwanted space is displaying in between the word (or) end of the word (or) starting of the word, this issue is occurring in the every 13th record of the file and in db there is no spaces.


Kindly any one can help me.
Posted
Updated 9-Oct-12 1:39am
v8
Comments
ridoy 22-Aug-12 1:53am
   
I think there is a space from where you read that data..see it carefully..if not then let us know..
Ajit Kumar Pal 9-Oct-12 7:58am
   
Can you tell how are you fetching these records in string builder.
I think appending is not done correctly .
surikarthi 9-Oct-12 8:07am
   
no its appending to string builder in correct way .

first i'm fetching data from db to the temporary table and from that defining to the business object(BO) using BO I'm appending to the string builder.
Ajit Kumar Pal 10-Oct-12 0:41am
   
which method are you using for appending is it Append or AppendLine?
surikarthi 10-Oct-12 1:29am
   
here i'm not using any properties of the string builder. using business object(i.e BO) i'm assigning to the string builder .

ex:

just I'm giving only word (i.e contractorsurname). and how I'm displaying in the text file.

StringBuilder contractorsurname = new StringBuilder(previewbo.Surname);

In above line "contractorsurname" is obj of String builder and "previewbo.Surname" this is BO its having data from db

for (int i = 0; i < 30; i++)
{
contractorsurname.Length = 30;
charsurname[i] = Convert.ToChar(contractorsurname[i]);
}

here charsurname[i] is the object of the char Arraylist, from string builder object we are assigning data to the array list and data should be 30 character so i have given limit i less than 30.


st.BaseStream.Seek(0, SeekOrigin.begin);
st.Write(charsurname, 0, contractorsurname.Length);


using above line I'm transferring data to the text file.
surikarthi 22-Aug-12 2:25am
   
check with below comments plz
surikarthi 22-Aug-12 2:27am
   
No, problem is not by record bcoz
every time it shows space in 82 line 56 position if i have been change the record also.


i have pasted some code how its writing to file



FileStream fs = new FileStream(AttachfileName, FileMode.Create);
//stream writer is used to write text.
StreamWriter st = new StreamWriter(fs);
st.BaseStream.Seek(0, SeekOrigin.Begin);
st.Write(dta, 0, illegal.Length);
st.BaseStream.Seek(3, SeekOrigin.End);

st.BaseStream.Seek(0, SeekOrigin.Current);
st.Write(contractoridentity, 0, contractoriden.Length);
st.BaseStream.Seek(4 - (contractoriden.Length), SeekOrigin.End);

st.BaseStream.Seek(0, SeekOrigin.Current);
st.Write(incometype1, 0, incometype.Length);
st.BaseStream.Seek(1 - (incometype.Length), SeekOrigin.End);

st.BaseStream.Seek(0, SeekOrigin.Current);
st.Write(chartfn, 0, tfn.Length);
st.BaseStream.Seek(9 - (tfn.Length), SeekOrigin.End);

st.BaseStream.Seek(0, SeekOrigin.Current);
st.Write(chardob, 0, contractordfb.Length);
st.BaseStream.Seek(8 - (contractordfb.Length), SeekOrigin.End);

st.Close();
fs.Close();

use Replace method.
C#
String.Replace(" ","");

Here am removing spaces in a string
   
Comments
AmitGajjar 22-Aug-12 1:17am
   
correct 5+
surikarthi 22-Aug-12 1:24am
   
i have used the above fn but it dnot work..
Santhosh Kumar Jayaraman 22-Aug-12 1:25am
   
post your code..
surikarthi 22-Aug-12 1:25am
   
my problem is while reading it doesnt show the space after displaying in .txt only it shows space in string and this is happen to only one record and remaining is displays fine...
Santhosh Kumar Jayaraman 22-Aug-12 1:26am
   
I believe that record should have a actual space.
Mohamed Mitwalli 22-Aug-12 1:35am
   
5+
surikarthi 22-Aug-12 1:48am
   
No, problem is not by record bcoz
every time it shows space in 82 line 56 position if i have been change the record also.


i have pasted some code how its writing to file



FileStream fs = new FileStream(AttachfileName, FileMode.Create);
//stream writer is used to write text.
StreamWriter st = new StreamWriter(fs);
st.BaseStream.Seek(0, SeekOrigin.Begin);
st.Write(dta, 0, illegal.Length);
st.BaseStream.Seek(3, SeekOrigin.End);

st.BaseStream.Seek(0, SeekOrigin.Current);
st.Write(contractoridentity, 0, contractoriden.Length);
st.BaseStream.Seek(4 - (contractoriden.Length), SeekOrigin.End);

st.BaseStream.Seek(0, SeekOrigin.Current);
st.Write(incometype1, 0, incometype.Length);
st.BaseStream.Seek(1 - (incometype.Length), SeekOrigin.End);

st.BaseStream.Seek(0, SeekOrigin.Current);
st.Write(chartfn, 0, tfn.Length);
st.BaseStream.Seek(9 - (tfn.Length), SeekOrigin.End);

st.BaseStream.Seek(0, SeekOrigin.Current);
st.Write(chardob, 0, contractordfb.Length);
st.BaseStream.Seek(8 - (contractordfb.Length), SeekOrigin.End);

st.Close();
fs.Close();
Zoltán Zörgő 22-Aug-12 2:23am
   
Why on earth are you doing with this code? Where are your fields coming from and how do you want to format them? I am pretty sure, you have not taken the correct way to do what you want.
Swinkaran 22-Aug-12 2:29am
   
If you reading from a text file, then it is very easy to manipulate the test file with "TextReader" class rather than using stream. see here http://msdn.microsoft.com/en-us/library/system.io.textreader.aspx
Have you tried with Trim[^], which eliminates leading and trailing whitespace.

Refer More:
Trim String in C#[^]

Have a look on detailed description on MSDN:
http://msdn.microsoft.com/en-us/library/t97s7bs3.aspx[^]
   
v2
Comments
Mohamed Mitwalli 22-Aug-12 1:35am
   
5+
C#
strText.tostring().replace(" ","");
   
v2
C#
string txt = "                   i am a string                                ";
txt = txt.Trim();
   

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900