Java Equivalent of C#'s Verbatim Strings with @

Java equivalent of C#'s verbatim strings with @

No. Escaping / externalizing the string is your only choice.

Can I convert a C# string value to an escaped string literal?

There's a method for this in Roslyn's Microsoft.CodeAnalysis.CSharp package on NuGet:

private static string ToLiteral(string valueTextForCompiler)
{
return Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral(valueTextForCompiler, false);
}

Obviously, this didn't exist at the time of the original question, but it might help people who end up here from Google Search.

Verbatim string literals v escape sequences

Any difference here is limited strictly to the compiler; the IL and runtime have no concept of verbatim vs escaped - it just has the string.

As for which to choose: whichever is more convenient ;p I almost always use verbatim string literals if there are unusual characters, as that allows for multi-line strings very easily and visually.

As an interesting case:

bool areSame = ReferenceEquals("c:\\somewhere", @"c:\somewhere"); // true

which tells are they are exactly the same string instance (thanks to "interning"). They aren't just equivalent; they are the same string instance to the runtime. It is therefore impossible that they can be (to the runtime) different in any way.

What does $ mean before a string?

$ is short-hand for String.Format and is used with string interpolations, which is a new feature of C# 6. As used in your case, it does nothing, just as string.Format() would do nothing.

It is comes into its own when used to build strings with reference to other values. What previously had to be written as:

var anInt = 1;
var aBool = true;
var aString = "3";
var formated = string.Format("{0},{1},{2}", anInt, aBool, aString);

Now becomes:

var anInt = 1;
var aBool = true;
var aString = "3";
var formated = $"{anInt},{aBool},{aString}";

There's also an alternative - less well known - form of string interpolation using $@ (the order of the two symbols is important). It allows the features of a @"" string to be mixed with $"" to support string interpolations without the need for \\ throughout your string. So the following two lines:

var someDir = "a";
Console.WriteLine($@"c:\{someDir}\b\c");

will output:

c:\a\b\c

What is the difference between a regular string and a verbatim string?

A verbatim string is one that does not need to be escaped, like a filename:

string myFileName = "C:\\myfolder\\myfile.txt";

would be

string myFileName = @"C:\myfolder\myfile.txt";

The @ symbol means to read that string literally, and don't interpret control characters otherwise.

How do I write a backslash (\) in a string?

The backslash ("\") character is a special escape character used to indicate other special characters such as new lines (\n), tabs (\t), or quotation marks (\").

If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string:

var s = "\\Tasks";
// or
var s = @"\Tasks";

Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.

Generally speaking, most C# .NET developers tend to favour using the @ verbatim strings when building file/folder paths since it saves them from having to write double backslashes all the time and they can directly copy/paste the path, so I would suggest that you get in the habit of doing the same.


That all said, in this case, I would actually recommend you use the Path.Combine utility method as in @lordkain's answer as then you don't need to worry about whether backslashes are already included in the paths and accidentally doubling-up the slashes or omitting them altogether when combining parts of paths.

Java Regex - capture string with single dollar, but not when it has two successive ones

You may write a little bit more verbose regex with multiple capturing groups and only grab those that are not null, or plainly concatenate the found group values since there will be always only one of them initialized upon each match:

%([^%.]+)%|(?<!\$)\$(?:\{([^{}]+)\}|([^$.]+))

See the regex demo.

Details

  • %([^%.]+)% - %, Group 1: one or more chars other than % and ., then a % is consumed
  • | - or
  • (?<!\$) - a negative lookbehind that matches a location in string that is not immediately preceded with $
  • \$ - a $
  • (?: - start of the non-capturing container group matching either of:

    • \{([^{}]+)\} - {, Group 2: any one or more chars other than { and }, then } is consumed
    • | - or
    • ([^$.]+) - Group 3: 1 or more chars other than $ and .
  • ) - end of the non-capturing container group.

Java usage:

String regex = "%([^%.]+)%|(?<!\\$)\\$(?:\\{([^\\{}]+)\\}|([^$.\\s]+))";
String string = "%ABC%\n$ABC.\n$ABC$XYZ ${ABC}\n\n$$EFG $${EFG}.";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = pattern.matcher(string);
List<String> results = new ArrayList<>();
while (m.find()) {
results.add(Objects.toString(m.group(1),"") +
Objects.toString(m.group(2),"") +
Objects.toString(m.group(3),""));
}
System.out.println(results); // => [ABC, ABC, ABC, XYZ, ABC]

Mind that in regular Java string literals, \ should be escaped (i.e. \\) to introduce a single literal backslash that is used as part of regex escapes.



Related Topics



Leave a reply



Submit