473,663 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1477
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.co m

"Michael A. Covington" <lo**@ai.uga.ed u.for.addresswr ote in message
news:uJ******** *******@TK2MSFT NGP05.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.c omwrote in
message news:2D******** *************** ***********@mic rosoft.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";

CodePrimitiveEx pression strExp = new CodePrimitiveEx pression(mystri ng);
CSharpCodeProvi der csharpcodeprovi der = new CSharpCodeProvi der();
StringWriter result = new StringWriter();
csharpcodeprovi der.GenerateCod eFromExpression (strExp, result, new CodeGeneratorOp tions());
return result.ToString ();
Regards
Niels Harremoës


>
"Michael A. Covington" <lo**@ai.uga.ed u.for.addresswr ote in message
news:uJ******** *******@TK2MSFT NGP05.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
7099
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 they different from regular strings? I've tried to look up in the python documentation but it just vaguely brushes through by saying that python raw strings are prefixed with "r" or "R". In PLY, "+" is "r'\+'" but "-" is "r'-'", why is there an...
3
32067
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
9744
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 numeric value according to an arbitrary regular expression.
10
9017
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 think!!). But I want to use malloc. Each string is at most 50 characters long, and there may be zero to thousands of lines. How do I actually start the array? I have seen char **array etc. At first I tried char *array but I think that gives 50...
3
16807
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 *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
30
17276
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 instance:- int i =2; char ch 'A'
74
3136
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
9679
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 handle strings, but just for general syntax e.g., puts print, expr math operations thanks
8
4269
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
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8548
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.