473,804 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New Line Constant Error!

Hi all,

i have a string,

str_old="E:\a\b \c.exe",

and i want to change to

str_new="E:\\a\ \b\\c.exe".

so now i use IndexOf() function to find the position of '\' , but it is
throwing error that a new line constant is present.

(i.e); int n=str_old.Index Of('\'); //error here..new line constant

how to correct this error??........ ..i would be happy if someone could help
me out...

thanks in advance,

with regards,
C.C.Chakkaradee p

Nov 16 '05 #1
3 38042
Chakkaradeep wrote:
Hi all,

i have a string,

str_old="E:\a\b \c.exe",

and i want to change to

str_new="E:\\a\ \b\\c.exe".

This would work:

string str_old = @"E:\a\b\c.exe" ;
string str_new = str_old.Replace (@"\", @"\\");
so now i use IndexOf() function to find the position of '\' , but it is
throwing error that a new line constant is present.

(i.e); int n=str_old.Index Of('\'); //error here..new line constant


As you are using a character literal, the line should look like this:

int n = str_old.IndexOf ('\\');

But as I stated earlier, you could just use String.Replace in this case.

Regards,
Joakim
Nov 16 '05 #2
In C# (as in C and C++), the backslash character ("\") has special
meaning: it is an "escape" that combines with the character after it to
make a special character that you can't type into your program. Some
famous uses of it, all the way back to C, are \n for a newline
(linefeed) control character, and \0 meaning the character with value 0
(the NUL control character).

So, any time you type a string and put a backslash \ in it, the
compiler thinks that you want to indicate a special character, and
tries to combine the backslash with the following character to create
something else.

However, what if you really want a backslash character in your string?
What if you want \n to mean not a newline control character, but really
a backslash followed by an en? Then you have to use a double-backslash,
\\, which the compiler reads as a special sequence that folds down into
a single backslash in the string itself.

Take a look at the following code:

string a = "\n";
string b = "\\n";

The string called "a" contains a _single character_: a newline, or
linefeed control character, because the sequence backslash-en is
interpreted by the compiler as a request for this special character.

The string called "b" contains _two characters_: a backslash, which the
compiler put into the string because it saw the "escaped backslash"
sequence: two backslashes in a row... followed by an en character.

Now in C and C++ this was all there was to it: everywhere in your
string that you wanted a single backslash character, you had to type
two into your program so that the compiler would know what to do. If
you wanted two, you would have to type four into your program, etc.
(Note that this is only the _compiler_ doing this, not the runtime. So,
if you read in a line from a file that has the characters "\n" on it,
these do _not_ get folded into a newline character, because the
_compiler_ isn't reading the file... your program is.)

However, because C# runs on the Windows operating system, and Windows
traditionally uses backslash to separate directory names in file paths,
Microsoft realized that forcing you to type double-backslashes for
every backslash you wanted in a hard-coded string in your program was
going to be... well, a pain in the ass. So, they added a new marker,
the "@" marker.

When you put an "@" at the beginning of a string, it means, "Compiler:
there are no special characters in this string. Don't do your normal
special-character processing on this one. Just put exactly what I type
into the string." So, the declaration for string "b" above could be
changed to:

string b = @"\n";

which produces the same thing internally: a two-character string
containing a backslash and an en.

In answer to your specific question, the compiler gives you an error
because you said:

int n = str_old.IndexOf ('\');

In this case, the backslash indicates a special character sequence, and
the following character is a single quote. That tells the compiler that
you really want a single quote mark in the character sequence, and that
the single quote that directly follows the backslash is _not_ the
ending delimiter for the character sequence. So, the compiler goes on
adding characters to the sequence (closed parenthesis, then semicolon)
until it arrives at the end of the line and realizes that something
went wrong. In effect, you never ended the character constant because
you told the compiler that the second single quote was "special" by
preceding it with a backslash.

As Joakim pointed out, you could fix this by either doubling the
backslash, a la C:

int n = str_old.IndexOf ('\\');

or adding the "@" marker to the start, to tell the compiler not to
treat the backslash specially:

int n = str_old.IndexOf (@'\');

Nov 16 '05 #3
Hi Joakim and Bruce,

Thanks a lot, i clearly understood this "new line thing" and i really thank
Bruce and Joakim for their Explanation.Goo d Job Guys..keep it up!

with regards,
C.C.Chakkaradee p

"Bruce Wood" wrote:
In C# (as in C and C++), the backslash character ("\") has special
meaning: it is an "escape" that combines with the character after it to
make a special character that you can't type into your program. Some
famous uses of it, all the way back to C, are \n for a newline
(linefeed) control character, and \0 meaning the character with value 0
(the NUL control character).

So, any time you type a string and put a backslash \ in it, the
compiler thinks that you want to indicate a special character, and
tries to combine the backslash with the following character to create
something else.

However, what if you really want a backslash character in your string?
What if you want \n to mean not a newline control character, but really
a backslash followed by an en? Then you have to use a double-backslash,
\\, which the compiler reads as a special sequence that folds down into
a single backslash in the string itself.

Take a look at the following code:

string a = "\n";
string b = "\\n";

The string called "a" contains a _single character_: a newline, or
linefeed control character, because the sequence backslash-en is
interpreted by the compiler as a request for this special character.

The string called "b" contains _two characters_: a backslash, which the
compiler put into the string because it saw the "escaped backslash"
sequence: two backslashes in a row... followed by an en character.

Now in C and C++ this was all there was to it: everywhere in your
string that you wanted a single backslash character, you had to type
two into your program so that the compiler would know what to do. If
you wanted two, you would have to type four into your program, etc.
(Note that this is only the _compiler_ doing this, not the runtime. So,
if you read in a line from a file that has the characters "\n" on it,
these do _not_ get folded into a newline character, because the
_compiler_ isn't reading the file... your program is.)

However, because C# runs on the Windows operating system, and Windows
traditionally uses backslash to separate directory names in file paths,
Microsoft realized that forcing you to type double-backslashes for
every backslash you wanted in a hard-coded string in your program was
going to be... well, a pain in the ass. So, they added a new marker,
the "@" marker.

When you put an "@" at the beginning of a string, it means, "Compiler:
there are no special characters in this string. Don't do your normal
special-character processing on this one. Just put exactly what I type
into the string." So, the declaration for string "b" above could be
changed to:

string b = @"\n";

which produces the same thing internally: a two-character string
containing a backslash and an en.

In answer to your specific question, the compiler gives you an error
because you said:

int n = str_old.IndexOf ('\');

In this case, the backslash indicates a special character sequence, and
the following character is a single quote. That tells the compiler that
you really want a single quote mark in the character sequence, and that
the single quote that directly follows the backslash is _not_ the
ending delimiter for the character sequence. So, the compiler goes on
adding characters to the sequence (closed parenthesis, then semicolon)
until it arrives at the end of the line and realizes that something
went wrong. In effect, you never ended the character constant because
you told the compiler that the second single quote was "special" by
preceding it with a backslash.

As Joakim pointed out, you could fix this by either doubling the
backslash, a la C:

int n = str_old.IndexOf ('\\');

or adding the "@" marker to the start, to tell the compiler not to
treat the backslash specially:

int n = str_old.IndexOf (@'\');

Nov 16 '05 #4

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

Similar topics

6
8629
by: paul calvert | last post by:
I hope somewhere here has encountered and solved a similar problem in the past. 1) on a new Win2000 PC: installed Visual C++ 6.0 download & install single file Service Pack 5.0 2) try to build my gui and dll projects, whose project, workspace, source files all resided on network drive mapped to H. The H mapping,
2
13295
by: Matt Morgan | last post by:
Problem description: I have a TextBox on Web User Control that is loaded into a PlaceHolder on my ASPX page. There are other Web User Controls on that page as well. When the page loads I can input text into the TextBox without any problem. However, after postback if I try to input text into the TextBox I get an Internet Explorer pop-up message box stating: 'A Runtime Error has occurred. Do you wish to debug?
1
4178
by: Andrew Phillipo | last post by:
I have some code that works everywhere but IE5.0, including IE5.5. Here is a snippet of where the code seems to go wrong: Location.prototype.change = function(current) { this.current = current; // refers to the currently selected estate agent var elem = dhtml_get_element(this.id()); // cross browser getElementById var oldElem = elem.parentNode; // we can't replace tables using innerHTML - get the parent element (a div)
4
3053
by: ben | last post by:
why is it, in the below code, when there's a printf statement (the one commented with /* ****** */) the final for loop prints out fine, but without the commented with stars printf statement included in the code there's a bus error on the fourth element in the final for loop? final for loop print out when the /* ****** */ printf line is included in the code: ab ba
3
5873
by: anagai | last post by:
hi I am trying to install the java bridge library for php 5.1.2. I have installed the j2see 1.4 and jdk. I have setup the section in php.ini as follows: java.classpath = "c:/PHP/ext/JavaBridge.jar;" java.java_home = "c:/Sun/AppServer/jdk/bin" java.libpath = "c:/PHP/ext"
2
3121
by: rajuk | last post by:
Hi i have following code,when i execute this code i got unterminated string constant error.any javascript guru can you look into this please. raju /* The link details */ var links = new Array ("link1", "link2", "link3"); var links_text = new Array ("Link 1", "Link 2", "Link 3"); var links_url = new Array ("link1.htm", "link2.htm","link3.htm"); /* Resolve the location */ var loc=String(this.location);
1
5957
by: VUNETdotUS | last post by:
Let's say I have a string: div.innerHTML = "<a onclick='foo(\""+myWord+"\");'></a>"; in IE only (tested version 7) if var myWord = "English" then it works fine but if var myWord = "Modifier Cha\u00EEnes" then I get "Unterminated string constant" error. What fix would you suggest to keep div.innerHTML = "" format?
1
3074
by: Andrew Poulos | last post by:
When I run a frame set locally I get the error A Runtime Error has occurred. Do you wish to Debug? Line: 1 Error: Invalid character Yes No
1
1669
renji1981
by: renji1981 | last post by:
Hi pals Hers my link Primus Infotech Solutions - IT Services , Product Engineering , Web Based Applications Here Im getting a Unterminated String constant Error When you click on the Services link it works ... but when i click back on the home link this dosent work .
0
9712
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
9594
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
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10089
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
9171
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...
1
7634
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
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...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.