how to do string manipulation in C#

azae

Member
Joined
Jan 6, 2005
Messages
18
hi all,
the last posting have solved by myself..

Now I am advancing to build program on smart device (pocket PC).
Function I want to do now is write to text file again.

Can I really declare my text file like this?
Code:
string fileName = "c:\\delivery.txt";
System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName,True);

OK..
I been trying to find out the String manipulation too...
Such in VB 6 there is Mid(0,hhh)
to limit the string size.
But How to do in C#?
 
On your first question:
That declaration seems right. It should open the c:\delivery.txt file for append. You can use the writer object to send text to it.

On the second. Look into the String object.
Instead of Mid(<start>, <length>), you can now use SubString:
String myString = "blabla";
String subString = myString.SubString(2, 2);

subString would contain "ab"

The String object has quite a few more handy methods ;).
 
Sorry its misconverzation there...

Sorry Wile.. what I really mean in string manipulation is actually string modifier....

I programming c.. we write
Code:
printf("%6d, 123");
so the output will be indent for 6 space to fit 123
I want to know how do we do that in C#.
Books I have here does not mention about this...

And the second thing is, is it the file system for pocket pc is the same as desktop pc?
I tried to run on emulator which include in VS.NET.
The program cause run time error.
 
I cant help you with the pocket pc filesystem, except saying that my pocket pc doesnt even have a C-root (c:\) so it is probably quite a bit different.


For printf/sprintf formatting thingies, there is String.Format (this is a static method, so use it on the String class, not on an object).

This uses { and } instead of the %. Your code would be something like String.Format("{0:6}", 123);

Quick copy & paste of a part of MSDN library about the format method gives another example:
If the value of format is, "Brads dog has {0,-8:G} fleas.", arg[0]is an Int16 with the value 42, (and in this example, underscores represent padding spaces) then the return value will be:

"Brads dog has 42______ fleas."
 
StreamWriter and StreamWriter are supported by the compact framework. From what I gather about the Pocket PC is that there is no C:\. To create a directory in the main \(root folder or My device folder equivalent to C:\ on yer PC) youd say
StreamWriter tw = new StreamWriter("\\myfile.txt");

to create a file in My documents youd say
StreamWriter tw = new StreamWriter("\\My Document\\mydoc.txt");
 
Last edited by a moderator:
Back
Top