473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Convert strings to/from C# syntax?

Is there a built-in way to convert strings to and from their syntactic
representation in C#?

For example, suppose I have a string containing 'a' and a newline character
and 'b'. Is there some built-in way to render this as a string containing
the characters 'a' '\' 'n' 'b' ?

And the inverse?

Sep 19 '07 #1
3 1468
Michael,

Do you mean you want a stirng literal like this:

"a\nb"

Or are you looking for a routine which will take the above literal and
give you the equivalent of:

"a\\nb"

Which when printed will give you "a\nb". If it is the latter, then
AFAIK, the answer is no, you would have to code this yourself.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:uJ***************@TK2MSFTNGP05.phx.gbl...
Is there a built-in way to convert strings to and from their syntactic
representation in C#?

For example, suppose I have a string containing 'a' and a newline
character and 'b'. Is there some built-in way to render this as a string
containing the characters 'a' '\' 'n' 'b' ?

And the inverse?
Sep 19 '07 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:2D**********************************@microsof t.com...
Michael,

Do you mean you want a stirng literal like this:

"a\nb"

Or are you looking for a routine which will take the above literal and
give you the equivalent of:

"a\\nb"

Which when printed will give you "a\nb". If it is the latter, then
AFAIK, the answer is no, you would have to code this yourself.
The latter. Thanks.

Sep 19 '07 #3
Michael,
>
Do you mean you want a stirng literal like this:

"a\nb"

Or are you looking for a routine which will take the above literal
and give you the equivalent of:

"a\\nb"

Which when printed will give you "a\nb". If it is the latter,
then AFAIK, the answer is no, you would have to code this yourself.
Well, it is probably much easier to code it yourself, but you could use the
CodeDOM, e.g.

string myString = "a\nb";

CodePrimitiveExpression strExp = new CodePrimitiveExpression(mystring);
CSharpCodeProvider csharpcodeprovider = new CSharpCodeProvider();
StringWriter result = new StringWriter();
csharpcodeprovider.GenerateCodeFromExpression(strE xp, result, new CodeGeneratorOptions());
return result.ToString();
Regards
Niels Harremoës


>
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:uJ***************@TK2MSFTNGP05.phx.gbl...
>Is there a built-in way to convert strings to and from their
syntactic representation in C#?

For example, suppose I have a string containing 'a' and a newline
character and 'b'. Is there some built-in way to render this as a
string containing the characters 'a' '\' 'n' 'b' ?

And the inverse?

Sep 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Maurice LING | last post by:
I'm trying to toy around with PLY (python lex-yacc) by David Beazley from University of Chicago and realized that the lex module uses python raw strings. What are python raw strings and how are...
3
by: Phd | last post by:
Hi, I'm writing a regex related program that lets the user supplies the regex definition. Is there an easy way to convert a string into a raw string? Cheers
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
3
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
30
by: ceeques | last post by:
Hi I am a novice in C. Could you guys help me solve this problem - I need to convert integer(and /short) to string without using sprintf (I dont have standard libray stdio.h). for...
74
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
2
by: Achillez | last post by:
Hi, I have a 10k+ line Tcl program that I would like to auto convert over to Python. Do any scripts exist that can convert ~90% of the standard Tcl syntax over to Python? I know Python doesn't...
8
by: Lawrence D'Oliveiro | last post by:
If triple-quoted strings had the Python-nature, then they would take indentation into account. Thus: """this is a multi-line string.""" would be equivalent to
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.