Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Update: The code works, error was caused by something else.
How do you find the index of a curly brace ({) in a string? I keep getting -1 even though the curly brace is there. Here's my code;
var str = "example string with {brace}.";
var index = str.IndexOf("{");
I've tried escaping the brace like this
var index = str.IndexOf("{{");
and like this
var index = str.IndexOf("{{}");
but it still returns -1
The code you've posted works just fine. Tested in LinqPad
var str = "example string with {brace}.";
var index = str.IndexOf("{");
returns 20.
There is no need to try and escape the { character. If you're for sure getting back -1 you have a different problem and will probably need to post more information/code for it to be resolved.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
string odp = "2,1";
bool abs = odp[1].Equals(",");
When I do Console.Writeline(odp[i]) it shows "," symbol so why does it equal to false?
I tried using == instead but it doesn't work either. Thanks in advance
A Char is encapsulated in single quotation marks ',' in C# (a String uses double quotation marks ",").
Try the below instead:
string odp = "2,1";
bool abs = odp[1].Equals(','); // true
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following regex string "(\"\--[a-zA-Z]+\")" but I can not use it since it uses a double quotation.
What are the escape characters I could try here?
I want to use as follows in C# code to filter the "KernalSize" from the following string :
parser.add_argument("--KernalSize", default = 6, help = "Noise removal")
Following filter (\"\--[a-zA-Z]+\") returns "--KernalSize". However, when I try to set the filter in C# code, it failed due to a double quotation sign within the string.
const string ParamNameFilter = #"(\"\--[a-zA-Z]+\")";
so your regex string (\"\--[a-zA-Z]+\") should match strings like "--AnythingAlpha"?
in reality the backslash before one of the - is not even necessary, so it begs a question if there is any type of a typo there, as it is equivalent to just (\"--[a-zA-Z]+\") or even a simpler (\"--[a-z]+\") with case-insensitive flag.
Now it's just about representing this string in C# strings:
(\"\--[a-zA-Z]+\") => to one of
#"(\""\--[a-zA-Z]+\"")"
"(\\\"\\--[a-zA-Z]+\\\")"
you can test them with something like:
public static void Main (string[] args)
{
Console.WriteLine(#"(\""\--[a-zA-Z]+\"")");
Console.WriteLine("(\\\"\\--[a-zA-Z]+\\\")");
}
The two quoting options are
Double quote "(\"--[a-zA-Z]+\")"
Verbatim #"(""--[a-zA-Z]+"")"
Note inside a verbatim string, the double quote is escaped by double quoting it.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
public string ReplaceTest(int i)
{
string rep = this.textBox3.Text;
string reped = rep.Replace("sir, this.textBox3.Text");
}
Like this? this is what i want to do but another error comes up not all code paths return a value
string.Replace() takes two arguments:
the string to replace
the string to use instead
If you want to remove "sir", it means to replace it with an empty string:
string reped = rep.Replace("sir", string.Empty);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am assigning variable to another variable but it throws error
string conf = "[32882914,32877390,32877377,32882917,32882922,32882926,32877379,32882887,32882891,32877382,32882897,32882902,32882907,32882909]"
int sku = 34444
string param1 = "{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{\"StyleId\":{0},\"SkuIds\":{1}}],\"RefreshSameDayDeliveryStore\":true}",sku,conf;
Error:
unexpected value string
varibale is already defined sku,conf
how to use variables in another variable in c#
You're missing a key part of the syntax, you need string.Format. Your code should instead read
string param1 = String.Format("{{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{{\"StyleId\":{0},\"SkuIds\":{1}}}],\"RefreshSameDayDeliveryStore\":true}}",sku,conf);
There are other ways to do this that you can use, for example you can use +
string param1 = "{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{\"StyleId\":" + sku + ",\"SkuIds\":" + conf + "}],\"RefreshSameDayDeliveryStore\":true}";
Or you could use the more similar interpolation
string param1 = $"{{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{{\"StyleId\":{sku},\"SkuIds\":{conf}}}],\"RefreshSameDayDeliveryStore\":true}}";
Notice in the first and third examples there are three curled brackets at the end of the second parameter instead of two, this is because two would escape to a literal "}" character, to instead get the syntax } followed by the literal "}", three must be used
To add on to what Alfie said, the back slashes (\) should also be escaped in C# with double back slashes.
"{{\\"SameDayDeliveryStoreNumber\\"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When using the following code,
string text = comboLanguages.SelectedItem.ToString();
string[] split = text.Split("\t", 1);
I get an error (below) on the 2nd line.
Error 1 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments MainWindow.xaml.cs 46 30
Can anyone please tell me what I'm doing wrong?
String.Split takes a char parameter, not a string. You needed to write:
string text = comboLanguages.SelectedItem.ToString();
string[] split = text.Split(new char[] {'\t'}, 1);
You need to change the code to:
string[] split = text.Split(new char[] { '\t' }, 1);
Note that the error is telling you exactly what the problem is.
If you are going to call this code regularly you might want to move the array declaration outside of the call to Split.