Visual Studio 2005: \a escape sequence appears in string but can't remove - visual-studio-2005

I'm working in J# with text extracted from MS Word using Excel interop. Right now I'm having a problem with text extracted from a cell. The cell is a row header, the visible text is "Total", but the extracted string is "Total\r\a". I want to remove the escape sequence, but VS won't detect or remove \a.
cellText = cellText.Replace("\r", ""); //works
cellText = cellText.Replace("\a", ""); //error: unrecognized escape sequence
cellText = cellText.Replace("\\a", ""); //doesn't remove the sequence
cellText.Contains("\\a") returns false.
Any ideas?

cellText = cellText.Replace("\a", ""); //error: unrecognized escape sequence
Well, \a is the standard escape sequence for C-like languages for the alert (bell) character, which usually makes a sound if sent to the console. But Java doesn't support it (although most of the others are ok). However, you can use a hex escape:
cellText = cellText.Replace("\u0007", "");
or an octal escape:
cellText = cellText.Replace("\007", "");
or if you're desperate to avoid the evil escape sequences:
char bell = 7;
cellText = cellText.Replace(bell.toString(), "");

Related

How to disable auto-created string in new VS 2022?

VS 2022 has a new "feature": when I hit enter in a quoted string literal, it splits the string so that there's a new partial string, + outside the string, a newline, and the rest of the string on the next line.
... that is, hitting enter while the cursor is between the 'u' and first 'f' turns
var foo = "stuff";
into
var foo = "stu" +
"ff";
How do I disable this "feature" (specifically for C#, but applying to all languages would be preferable)?
Specifically: when I hit enter, I want a plain ol' newline. If that makes the code invalid, so be it: I can decide whether to remove the newline, use string concatenation, use a multi-line string literal, or whatever else may be needed to fix the code.

regex with quotation mark

How do I use .js to regex out the quotation marks in the following string?
var wtfx = "<div>ExternalClass=5"</div>44FB";
var wtf = /<div>ExternalClass.*>/;
wtf = wtfx.replace(wtf, "");
alert(wtf);
shows this does not work. If I take the '"' out then it does. How do I 'escape' the quote?
for example I'd like to use reg ex on the above wtf string to yield only the string 44FB.
not getting this.
The following Code will alert "44FB":
var wtfx = "<div>ExternalClass=5\"</div>44FB";
var wtf = /<div>ExternalClass.*>/;
wtf = wtfx.replace(wtf, "");
alert(wtf);
The only change is the Backslash before the Quotationmark in line 1.
However, I'm not 100% sure if that is what you want. Feel free to write a comment if you need another result.

Reformatting dates

I'm trying to reformat German dates (e.g. 13.03.2011 to 2011-03-13).
This is my code:
str = "13.03.2011\n14:30\n\nHannover Scorpions\n\nDEG Metro Stars\n60\n2 - 3\n\n\n\n13.03.2011\n14:30\n\nThomas Sabo Ice Tigers\n\nKrefeld Pinguine\n60\n2 - 3\n\n\n\n"
str = str.gsub("/(\d{2}).(\d{2}).(\d{4})/", "/$3-$2-$1/")
I get the same output like input. I also tried my code with and without leading and ending slashes, but I don't see a difference. Any hints?
I tried to store my regex'es in variables like find = /(\d{2}).(\d{2}).(\d{4})/ and replace = /$3-$2-$1/, so my code looked like this:
str = "13.03.2011\n14:30\n\nHannover Scorpions\n\nDEG Metro Stars\n60\n2 - 3\n\n\n\n13.03.2011\n14:30\n\nThomas Sabo Ice Tigers\n\nKrefeld Pinguine\n60\n2 - 3\n\n\n\n"
find = /(\d{2}).(\d{2}).(\d{4})/
replace = /$3-$2-$1/
str = str.gsub(find, replace)
TypeError: no implicit conversion of Regexp into String
from (irb):4:in `gsub'
Any suggestions for this problem?
First mistake is the regex delimiter. You do not need place the regex as string. Just place it inside a delimiter like //
Second mistake, you are using captured groups as $1. Replace those as \\1
str = str.gsub(/(\d{2})\.(\d{2})\.(\d{4})/, "\\3-\\2-\\1")
Also, notice I have escaped the . character with \., because in regex . means any character except \n

Converting Characters to ASCII Code & Vice Versa In C++/CLI

I am currently learning c++/cli and I want to convert a character to its ASCII code decimal and vice versa( example 'A' = 65 ).
In JAVA, this can be achieved by a simple type casting:
char ascci = 'A';
char retrieveASCII =' ';
int decimalValue;
decimalValue = (int)ascci;
retrieveASCII = (char)decimalValue;
Apparently this method does not work in c++/cli, here is my code:
String^ words = "ABCDEFG";
String^ getChars;
String^ retrieveASCII;
int decimalValue;
getChars = words->Substring(0, 1);
decimalValue = Int32:: Parse(getChars);
retrieveASCII = decimalValue.ToString();
I am getting this error:
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
Any Idea on how to solve this problem?
Characters in a TextBox::Text property are in a System::String type. Therefore, they are Unicode characters. By design, the Unicode character set includes all of the ASCII characters. So, if the string only has those characters, you can convert to an ASCII encoding without losing any of them. Otherwise, you'd have to have a strategy of omitting or substituting characters or throwing an exception.
The ASCII character set has one encoding in current use. It represents all of its characters in one byte each.
// using ::System::Text;
const auto asciiBytes = Encoding::ASCII->GetBytes(words->Substring(0,1));
const auto decimalValue = asciiBytes[0]; // the length is 1 as explained above
const auto retrieveASCII = Encoding::ASCII->GetString(asciiBytes);
Decimal is, of course, a representation of a number. I don't see where you are using decimal except in your explanation. If you did want to use it in code, it could be like this:
const auto explanation = "The encoding (in decimal) "
+ "for the first character in ASCII is "
+ decimalValue;
Note the use of auto. I have omitted the types of the variables because the compiler can figure them out. It allows the code to be more focused on concepts rather than boilerplate. Also, I used const because I don't believe the value of "variables" should be varied. Neither of these is required.
BTW- All of this applies to Java, too. If your Java code works, it is just out of coincidence. If it had been written properly, it would have been easy to translate to .NET. Java's String and Charset classes have very similar functionality as .NET String and Encoding classes. (Encoding to the proper term, though.) They both use the Unicode character set and UTF-16 encoding for strings.
More like Java than you think
String^ words = "ABCDEFG";
Char first = words [0];
String^ retrieveASCII;
int decimalValue = ( int)first;
retrieveASCII = decimalValue.ToString();

How to remove a ^M character java

Problem:
If String ends with \r, remove \r
I started with something like this
if (masterValue.endsWith(CARRIAGE_RETURN_STR)) {
masterValue = masterValue.replace(CARRIAGE_RETURN_STR, "");
}
where
public static final String CARRIAGE_RETURN_STR = (Character.toString(Constants.CARRIAGE_RETURN));
public static final char CARRIAGE_RETURN = '\r';
This seems awkward to me.
Is there an easy way to just remove \r character?
I then moved on to this:
if (value.contains(CARRIAGE_RETURN_STR)) {
value = value.substring(0, value.length()-3);
//-3 because we start with 0 (1), line ends with \n (2) and we need to remove 1 char (3)
But this too seems awkward .
Can you suggest a easier, more elegant solution?
Regexes can support end-of-string anchoring, you know. (See this Javadoc page for more information)
myString.replaceAll("\\r$", "");
This also takes care of fixing \r\n --> \n, I believe.
I'd write it like this:
if (masterValue.endsWith("\r")) {
masterValue = masterValue.substring(0, masterValue.length() - 1);
}
I see no point in creating a named constant for the String "\r".
By the way, your second attempt is incorrect because:
String.contains("\r") tells you if the String contains a carriage return, not if it ends with a carriage return,
the second argument of String.substring(int, int) is the index of the end character; i.e. the position first character that should NOT be in the substring, and
the length of "\r" is one.

Resources