Click here to Skip to main content
15,891,621 members
Home / Discussions / C#
   

C#

 
AnswerRe: String with escaped squences... Pin
Alaric_21-Sep-06 11:41
professionalAlaric_21-Sep-06 11:41 
AnswerRe: String with escaped squences... Pin
Christian Graus21-Sep-06 11:45
protectorChristian Graus21-Sep-06 11:45 
AnswerRe: String with escaped squences... Pin
Alaric_21-Sep-06 11:49
professionalAlaric_21-Sep-06 11:49 
AnswerRe: String with escaped squences... Pin
AndriyZZ21-Sep-06 12:15
AndriyZZ21-Sep-06 12:15 
AnswerMore Details: String with escaped sequences... Pin
e-laj21-Sep-06 13:03
e-laj21-Sep-06 13:03 
GeneralRe: More Details: String with escaped sequences... Pin
Christian Graus21-Sep-06 15:27
protectorChristian Graus21-Sep-06 15:27 
GeneralRe: More Details: String with escaped sequences... Pin
e-laj21-Sep-06 16:33
e-laj21-Sep-06 16:33 
AnswerOK: Here is a method to handle that... Pin
e-laj21-Sep-06 17:53
e-laj21-Sep-06 17:53 
First, thank you all for trying to help me, as i said, it seems that there is no built in method for escape sequences substitution.
Hence i set down and wrote the following method, hopefully with no bugs.
In the method i use scanning instead of serching and replacing as well as some bitwise operations in order to enhance performance.
The reverse method is much simpler to code.

Finally, here in Israel we celebrate a new year - so happy new year to you all.

Here is the method.


/// <summary>
/// Substitutes the escape characters in the specified string with their appropriate characters.
/// </summary>
/// <param name="str">The string.</param>
/// <returns>The string after the escape characters were replaced by the appropriate character.</returns>
/// <remarks>
/// If the method encounter invalid escape character, the backslash character is removed and
/// the character that comes right after it is copied with no change.
/// </remarks>
public static string SubstituteEscapes(string str)
{
StringBuilder sbResult = new StringBuilder();
bool bEscaped = false;

for(int i = 0; i < str.Length; i++)
{
char current = str[i];
if(bEscaped)
{
switch(current)
{
case '\'':
case '\"':
case '\\':
/* Merely append the current character. This is the default behavior */
sbResult.Append(current);
break;

case '0':
/* The '\0' (string terminator) character \u0000 */
sbResult.Append('\0');
break;

case 'a':
/* The bell (alarm) character \u0007*/
sbResult.Append('\a');
break;

case 'b':
/* The backspace character \u0008*/
sbResult.Append('\b');
break;

case 'f':
/* The form feed character \u000C*/
sbResult.Append('\f');
break;

case 'n':
/* The line feed character \u000A*/
sbResult.Append('\n');
break;

case 'r':
/* The carriage return character \u000D*/
sbResult.Append('\r');
break;

case 't':
/* The tab character \u0009*/
sbResult.Append('\t');
break;

case 'v':
/* The vertical tab character \u000B*/
sbResult.Append('\v');
break;

case 'u':
/* Unicode character (exactly four digits to read on) */
try
{
char ch = (char)int.Parse(str.Substring(i + 1, 4), NumberStyles.AllowHexSpecifier);
sbResult.Append(ch);
i += 4;
}
catch
{
sbResult.Append(current);
}
break;

case 'x':
/* hexadecimal (1 digit up to 4 digits to read on) */
int j = 0; /* cout how many digit were scanned */
int value = 0; /* hold the value */
do
{
try
{
int curr = int.Parse(str[i + j + 1].ToString(), NumberStyles.AllowHexSpecifier);
value = ((value << 4) | curr);
j++;
}
catch
{
break;
}
} while(j < 4);
if(j != 0)
{
sbResult.Append((char)value);
i += j;
}
else
{
sbResult.Append(current);
}
break;

default:
/* Unrecognized escape chracter */
sbResult.Append(current);
break;

}
bEscaped = false;
}
else
{
if(current == '\\')
{
bEscaped = true;
}
else
{
sbResult.Append(current);
}
}
}
return sbResult.ToString();
}

AnswerRe: String with escaped squences... Pin
Rob Graham21-Sep-06 17:54
Rob Graham21-Sep-06 17:54 
GeneralRe: Finally found the method Pin
e-laj21-Sep-06 19:02
e-laj21-Sep-06 19:02 
Questionbinding a listbox Pin
heze21-Sep-06 11:18
heze21-Sep-06 11:18 
Answerself response Pin
heze21-Sep-06 11:36
heze21-Sep-06 11:36 
QuestionWhat is the best choice of this day for NATIVE Windows programming ? Pin
Nadia Monalisa21-Sep-06 11:03
Nadia Monalisa21-Sep-06 11:03 
AnswerRe: What is the best choice of this day for NATIVE Windows programming ? Pin
AndriyZZ21-Sep-06 12:22
AndriyZZ21-Sep-06 12:22 
AnswerRe: What is the best choice of this day for NATIVE Windows programming ? Pin
Christian Graus21-Sep-06 12:24
protectorChristian Graus21-Sep-06 12:24 
AnswerRe: What is the best choice of this day for NATIVE Windows programming ? Pin
Nader Elshehabi21-Sep-06 19:26
Nader Elshehabi21-Sep-06 19:26 
QuestionP2P file sharing and messaging Pin
asamay21-Sep-06 10:22
asamay21-Sep-06 10:22 
AnswerRe: P2P file sharing and messaging Pin
Ri Qen-Sin21-Sep-06 10:35
Ri Qen-Sin21-Sep-06 10:35 
AnswerRe: P2P file sharing and messaging Pin
Nader Elshehabi21-Sep-06 19:50
Nader Elshehabi21-Sep-06 19:50 
GeneralRe: P2P file sharing and messaging Pin
asamay22-Sep-06 9:30
asamay22-Sep-06 9:30 
QuestionTyped DataSets Pin
eggsovereasy21-Sep-06 10:05
eggsovereasy21-Sep-06 10:05 
QuestionProblem: Form Controls ghost when form moves [modified] Pin
Alaric_21-Sep-06 9:02
professionalAlaric_21-Sep-06 9:02 
AnswerRe: Problem: Form Controls ghost when form moves Pin
Nader Elshehabi21-Sep-06 19:46
Nader Elshehabi21-Sep-06 19:46 
GeneralRe: Problem: Form Controls ghost when form moves Pin
Alaric_22-Sep-06 5:42
professionalAlaric_22-Sep-06 5:42 
GeneralRe: Problem: Form Controls ghost when form moves Pin
Nader Elshehabi22-Sep-06 8:50
Nader Elshehabi22-Sep-06 8:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.