This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I have this String
link="https%3a%2f%2fen.wikipedia.org%2fwiki%2fHuawei/"
which shoud be like this:
link="https://en.wikipedia.org/wiki/Huawei/"
I wrote this code:
link.Replace("%2f", "/");
link.Replace("%3a", ":");
But it did not work.
Instead of trying to decode the URL yourself I'd use HttpUtility.UrlDecode
HttpUtility.UrlDecode("https%3a%2f%2fen.wikipedia.org%2fwiki%2fHuawei/")
"https://en.wikipedia.org/wiki/Huawei/"
See: https://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode(v=vs.110).aspx
String.Replace does return the value replaced try:
link = link.Replace("%2f", "/");
link is a string and is not mutating when you call the Replace method
link.Replace will not affect the link object itself, instead it returns a new String
from the doc emphasis mine:
Returns a new string in which all occurrences of a specified Unicode
character in this instance are replaced with another specified Unicode
character.
do instead:
link = link.Replace("%2f", "/"); or
link = link.Replace("%3a", ":");
Related
This question already has answers here:
What is a word boundary in regex?
(13 answers)
Closed 3 years ago.
Below is a tiny method to basically replace the word "Stack" with ".png" in a string. So something called "Grid01Stack" would return as "Grid01.png" however the operation doesn't do anything at all, the string remains the same. What is going wrong? Here is the code:
private string GetUriFromName(string GridName)
{
string result = Regex.Replace(GridName, #"\bStack\b", ".png");
return (#"Resources/Images/"+result);
}
While you can simply do, per Yuri and Cid's suggestions:
GridName.Replace("Stack",".png")
This is not the best option if the word 'Stack' will ever appear more than once in the string, as it will replace all instances. So, for example, "Stack01Stack" will become ".png01.png". As you are trying to form a good filename, you really only want to replace the last occurence of "Stack" with ".png", and only if it at the end of the string. Therefore, using "Stack\b" as the comments suggested could end up messing with valid filenames as well, if Stack shows up multiple times. For instance, using that Regex "GridStack-01Stack" will become "Grid.png-01.png"
This is all based on speculation of what these strings might be, so this solution might not be necessary, but I'd recommend the following Regex, which will only change the word Stack if it occurs at the end of the string:
string result = Regex.Replace(GridName, "Stack$", ".png");
\bStack**\b** - is looking for the whole word 'Stack' with spaces, tab, line break, etc before and after word.
You just need the String.Replace for you case.
• String replace: string x = "Grid01Stack".Replace("Stack", ".png");
• Regex: string x = Regex.Replace("Grid01Stack", "[Ss]tack$", ".png");
The regex will search for Stack or stack which are always in the end of the string.
This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Convert C# string to JavaScript String
(3 answers)
Closed 4 years ago.
Salaam
I am looking for a proper version of a C# or Razor equivalent of PHP's addSlashes. That would add
\ to some\string => some\\string
Please provide help
Why I needed this
In my application a user entered Sometext in textbox was accidently pressed next time when page when data was populated though Razor it was like this
...append('<span>'+'#Model.value'+'</span>')
=> after compiling it becomes like this
...append('<span>'+'sometext\'+'</span>')
so with this scenario my javascript code broke at '\' because now single quote has started but not ending due to ``. So i thought instead of limiting characters i would rather add slashes through C# code
Thank You
You don't show any code you've already written, but this can be done by using [string.replace()] ( https://www.w3schools.com/jsref/jsref_replace.asp ) :
var str = "This is \\a test";
var replaced = str.replace("\\", "\\\\");
Whoops - you want the answer in C# , I misread your "javascript" tag. It's mostly the same:
string str = "This is \\a test";
string replaced = str.Replace("\\", "\\\\");
Also see C# String Replace
After the update, https://stackoverflow.com/a/27574931/34092 is most likely a much better answer.
This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 5 years ago.
Sometimes I find that many people use #string for locate a directory than use string even I though that they are same.
for example :
I have variable that called direct
string direct = "C:\\Users";
And then,I type :
System.IO.Directory.CreateDirectory(#direct);
I think it's same with
System.IO.Directory.CreateDirectory(direct);
But,what's difference between #direct and direct?
The # prefix allows you to use a reserved keyword as a variable name.
The following is an error:
string string = "C:\\Junk";
but the following is allowed (although a very bad idea):
string #string = "C:\\Junk";
The # prefix can also be used as a verbatim string literal:
string thefolderpath = #"C:\Junk";
If I had to guess, I would say the original programmer decided to use #string, #int etc. for temporary variables, as a style choice. A style choice, it must be said, akin to wearing socks with sandals.
This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 5 years ago.
In this scenario the data I could have in my string may look like below but keep in mind the ids are dynamically generated so this isn't static and could be more than 2 if you haven't caught onto that.
ing:server blah blah, you. 2019,;:10-!gs.csd
1. id=value, otherid=value, pos=(22,22,33)
2. id=value2, otherid=value2, pos=(2g,2g,f) info other info info info info etc etc.
EDIT: How am I supposed to extract the individual values into strings afterwards from the string, the following does not work:
String valueString = "csd 1. id=value, otherid=value, pos=(22,22,33) ";
String value = valueString.Substring(valueString.IndexOf("otherid"), valueString.IndexOf(",") - valueString.IndexOf("otherid"));
You can do with Substring since you have already way of expecting when to start and when to end on your searching.
string result = x.Substring(x.IndexOf("csd"), (x.IndexOf("info ") - x.IndexOf("csd")));
I start searching on the start of the word "csd" and ends with the word "info " (with space), since there is also a word of info at the beginning of your string.
The result would be:
"csd 1. id=value, otherid=value, pos=(22,22,33) 2. id=value2, otherid=value2, pos=(24,21,33) "
This question already has answers here:
Can I expand a string that contains C# literal expressions at runtime
(5 answers)
Closed 6 years ago.
I have a literal as a string in a file
def s_CalculatePartiallyUsedTechPenalty(rate):\n total = min(rate,0)\n title = \"Partially Used Technology Penalty\" \n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)
when reading the file the text obviously looks like this:
def s_CalculatePartiallyUsedTechPenalty(rate):\\n total = min(rate,0)\\n title = \\\"Partially Used Technology Penalty\\\" \\n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)
Is there clean way to convert this string so that the value in the file is also the actual value of the string in code. In other words that that \n for example is \n and not \\n.
I understand that I can write a method that goes and replaces all the applicable values, but I do not want to do that unless it is the only way.
Edit: In response to John Wu's answer. No I am not confused. I do understand exactly that this is happening however I want to convert the literal value "\n" to the newline character. So instead of the literal value of \n it should be a new line.
Basically the inverse of How to convert a string containing escape characters to a string
You are confusing yourself. The string held in the file will be exactly the same as the string held in a string variable obtained by reading the file.
Perhaps you are using Visual Studio to inspect the string (i.e. using the Watch window or just hovering over the variable while the code is in debug mode). In this case, Visual Studio will display the extra slash to indicate that the string variable contains the literal value "\n" instead of a newline character.
If you want to eliminate the escape characters in the Watch window, you can append the format specifier ,nq to the variable name (link).
See also this question on StackOverflow.
If you can not fix file-writing code, that you can solve this issues in a following way:
String.Replace(#"\\\", #"\");
String.Replace(#"\\", #"\");
Or, in case, if you normal unescaped string,
String.Replace(#"\\\""", "\"");
String.Replace(#"\\n", Environment.NewLine);
P.s. Also think about other special characters, like \t
UPDATED:
Even better approach:
class Program
{
static void Main(string[] args)
{
var escaped = #"def s_CalculatePartiallyUsedTechPenalty(rate):\n total = min(rate,0)\n title = \""Partially Used Technology Penalty\"" \n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)";
var unescaped = Regex.Unescape(escaped);
Console.WriteLine(unescaped);
}
}