3 questions... (string related)

G

Guest

Hi,

I'm writing a C# Windows Application and i have 3 questions:

1. I have a string which i want to write into file but i want to write it in
ASCII format, how can i do it?

2. I need my string to be 120 chars length, incase it's less then 120 chars,
i need to fill it with spaces, is there better way to do it then:

while(str.length<120)
{
str=str+" ";
}

3.I need to concatenate few string into one string, but i need to keep the
orginal order, my string contains numbers, english letters, hebrew letters
and white spaces. when i'm using str=str1+str2 the order is mixed (the hebrew
and english letters along with the numbers), is it better way to concatenate
my strings to one string that keep the order of my strings?

Thanks,
Gidi.
 
L

Lars Behrmann

Hi Gidi,

for your first question (set the encoding information):

StreamWriter sw = new StreamWriter(string path, true,
System.Text.Encoding.ASCII);

for your second question (use PadRight() method):

string mystring = "Hello";
if(mystring.Length < 120)
mystring.PadRight(120, new char(' '));

For your third question, use an ArrayList and insert
the strings in the right order, after that concate
them by foreach with +=

Cheers
Lars Behrmann

_________________
Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
 
G

Guest

Hi Lars,

Thanks for your fast answer.
I tried to use:
StreamWriter sw=new StreamWriter(string path,true,System.Text.Encoding.ASCII);
and now instead of hebrew letters i see in the file ???? (before that i saw
it as gibberish), do u have any idea why?

Thanks Again,
Gidi.
 
M

Mark Rae

StreamWriter sw=new StreamWriter(string
path,true,System.Text.Encoding.ASCII);
and now instead of hebrew letters i see in the file ???? (before that i
saw
it as gibberish), do u have any idea why?

I wonder if it's because the Hebrew character set does not fit into the
"standard" ASCII range i.e. from 32 to 127...?
 
J

Jon Skeet [C# MVP]

Mattias Sj?gren said:
And the second line should be

mystring = mystring.PadRight(120, ' ');

Or preferrably just

mystring = mystring.PadRight(120);

Space is used as the default character to pad with :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top