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) "
Related
This question already has an answer here:
How to replace substring in a string in c#
(1 answer)
Closed 1 year ago.
I imagine this being a very weird question:
I'm working in C#, and I have a string, containing exactly one character, ASCII-code 6.
You can see this in the Watch-window:
current "\u0006" string
Now I have this piece of code:
if (current.IndexOf("\u0006") != -1)
current.Replace("\u0006",string.Empty);
if (current != "") // <= I am here and 'current' still is just "\u0006"
My code went into the if-clause, so the "\u0006" is recognised, but for some reason it doesn't get removed.
What am I doing wrong here?
As string is immutable it is not being changed. When calling Replace, it returns a new string but doesn't update the current one.
current = current.Replace("\u0006",string.Empty);
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:
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", ":");
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);
}
}