Searching a string for a substring containing operators
I'm trying to find how many times the string
"*"
occurs in a string I have.
I've tried a few different things including backslashes, but I found a stackoverflow solution saying a string literal should work:
var matches = response.Where(s => s.Contains(@""*"")).Count;
I'm met with an error Operator * cannot be applied to operands of type 'string' and 'string'
. What's the best practice here to accomplish this?11 Replies
@""*""
doesn't make any sense
It reads as (@"") * ("")
, and you can't multiply strings
Just use "*"
, no need for the verbatim string.thinker227#5176
REPL Result: Success
Result: int
Compile: 603.664ms | Execution: 61.216ms | React with ❌ to remove this embed.
or this
Hey, thanks for replying. To clarify, the substring I am looking for is "*", as opposed to just the char *
The char
*
?
Discord's formatting is weirdexample string:
"I'm searching for "*" in this string"
method should return a count of 1Ah
"*"
as a stringyep! sorry hard to convey
Yeah, you have to escape the
"
s or use a verbatim string. "\"*\""
or @"""*"""
.
In verbatim strings (preceded by the @), " characters are written as ""
Otherwise you just escape them using \"
ahh so I was one set of "" off on the string literal. Thanks for the help!
Yeah, no problem you can type
/close
to close the post.