I have this line of code:
var defaultResult = $"Enum_{#this.GetType().Name}_{#this}";
but I get this error:
expected ;
How do I fix this? String.Format?
The $ is a feature available in C# 6.0. Make sure you're set to the correct version.
String interpolation is supported onward c# 6.0.
read here for more
Related
I don't understand why this will not compile when typed out in a .cshtml/Razor page:
#($"{"\""}") <-- this does not work
#($"{"'"}") <-- this also does not work
#($"{"a"}") <-- this DOES work
The error states:
RZ1000: Unterminated string literal. Strings that start with a quotation mark (") must be terminated before the end of the line. However, strings that start with # and a quotation mark (#") can span multiple lines.
CS1002: ; expected
CS1513: } expected
It appears that non-quote characters work fine, but a double-quote or single-quote character breaks Razor's ability to parse the interpolated string, even when the quote character is escaped.
All the examples work fine in a plain .cs file:
public sealed class Test
{
public string x = $"{"\""}";
public string y = $"{"'"}";
public string z = $"{"a"}";
}
What gives?
My environment is as follows:
.NET SDK: v7.0.102
.NET Runtime: v7.0.2
C#: v11 (preview)
Visual Studio Community 2022 (64-bit): v17.4.4
It's a bug, known by the .NET guys as far back as 2015 at least.
The dotnet engineers on the dotnet/aspnetcore github page felt the work required to fix it outweighed the benefits, however:
Jun 3, 2015
After investigating this issue further turns out the requirements to complete this are pretty high (need to understand C# close to entirety at the Tokenizer level). Will revisit this later.
2 months later...
Aug 14, 2015
Moving to backlog because we feel these cases are not common in MVC views, and have trivial workarounds (e.g. use string.Format() instead of string interpolation).
https://github.com/dotnet/aspnetcore/issues/4976
I would like to know how to accomplish the
PHP
query = rtrim($query, '& ');
in C# ASP .NET MVC
I have tried Strings.RTrim("string") but it does not work.
Have also tried query = query.Trim(query, "&"); but it gives me error.
Also msdn provides code for rtrim in visual basic, which does not help.
From looking at the code it seems like its' trimming the whitespace if found after & character.
Anyone know how to replicate the php command?
Cheers
The .Trim() method removes whitespaces from both the start and end of the string, so
" Hello World ".Trim() -> "Hello World"
For performing the RTrim() you can use the TrimEnd() method with the String.
" Hello World ".TrimEnd() -> " Hello World"
C# Has pretty neat String trimming methods.
I'm listing different options below. Although, the one you are looking for is
StringVariable.TrimEnd();
"This string ends here. ".TrimEnd(); //This also works.
String-trimming methods
string message = " Hello! ";
message.TrimEnd(); // Trims from the right side.
message.TrimStart(); // Trims from the left side.
message.Trim(); // Trims on both sides
Summary
message.TrimEnd() will trim from the right side of the string variable.
message.TrimStart() will trim from the left side of the string variable.
message.Trim() will trim from both sides of the string variable.
Also msdn provides code for rtrim in visual basic, which does not help.
I found a few that might help you, and they have them in multiple .NET languages, including C#.
Microsoft - String.Trim Method
Microsoft - String.TrimEnd Method
Microsoft - String.TrimStart Method
Hope this helps you :)
try this ==>
string.Remove(string.LastIndexOf("&"));
With the help of remove last index you can remove the last element
Using .NetCore 1.1.2.
After successfully getting results from a search via Azure Search SDK, I am trying to decode the metadata_storage_path value. I've seen people saying to use HttpServerUtility.UrlTokenDecode in .NET or an equivalent in other languages as seen here.
Then the question becomes, what is the equivalent in .NetCore of HttpServerUtility.UrlTokenDecode? With:
var pathEncoded = "aHR0cHM6Ly9mYWtlZC5ibG9iLmNvcmUud2luZG93cy5uZXQvcGRmYmxvYnMvYW5udWFsX3JlcG9ydF8yMDA5XzI0NTU20";
I have tried the following:
var pathbytes = Convert.FromBase64String(pathEncoded);
//Throws System.FormatException "Invalid length for a Base-64 char array or string."
and
var pathbytes = WebEncoders.Base64UrlDecode(pathEncoded);
//Throws System.FormatException - "TODO: Malformed input."
Interestingly enough, everything works just fine if I cut off the last charater in pathEncoded... What is the proper way to handle this situation with Microsoft.AspNetCore 1.1.2?
HttpServerUtility.UrlTokenEncode appends an extra trailing character to the encoded string. You're doing it right - just remove that extra character and use WebEncoders.Base64UrlDecode. See this Q&A for details.
I used the following function in asp.net core 2.1 to encode the meta_storage_path value from Azure search.
private string DecodeBase64String(string encodedString)
{
var encodedStringWithoutTrailingCharacter = encodedString.Substring(0, encodedString.Length - 1);
var encodedBytes = Microsoft.AspNetCore.WebUtilities.WebEncoders.Base64UrlDecode(encodedStringWithoutTrailingCharacter);
return HttpUtility.UrlDecode(encodedBytes, Encoding.UTF8);
}
I just wanted to add that you can also deselect the 'Base-64 Encode Keys' Azure search indexer option.
NOTE: Only do this for fields with no characters Azure considers invalid for document keys.
I'm converting some VB code in C#, i have this if sentence
if param and 1 then
...
end if
i don't get how to convert this in C# (btw param is a short), is this something related to power of 2? How can i convert this?
You need to write this as a boolean expression:
if ((param & 1) != 0)
{
..
}
I would use a code convert to migrate you code across try the following site:
http://converter.telerik.com/
I have a class in PHP that encodes this code "?�m�U", using urlencode(), resulting in "%3F%B6%16m%BEU". However, when I try to encode with c# uisng HttpUtility.UrlEncode(), the result is not the same. The method in c# has a second parameter "Encoding.SOMETHING". I've tried each one of the possible paramaters, but it still doesn't work.
Does anyone know how I can fix this?
This one should work:
string utf8Encoded = System.Web.HttpUtility.UrlEncode(YOURURL, Encoding.UTF8);