❔ LINQ
Does linq have its own replace method? If so, does it make a new string or modify? Also is there a way to replace a string without making new string?
21 Replies
Wym a replace method?
And no, strings are immutable
So unless you get into the
unsafe
territory and start swapping bytes around... no, you can't change a string without making a new string.replace()
Yeah, and why would LINQ need this method? What would you want to replace with it?
Because string already does have a replace method
What's your starting point? A list of strings?
yeah like words
i mean it's okay I think i know a way to work around something anyway
And you want to replace every
"dog"
into "cat"
or some such?
["dog", "is", "dog"]
into ["cat", "is", "cat"]
for example?yeah but i have it an a loop to find different words and it creates a new string everytime. so it would just return the latest string. The thing is I would want it to add on top of each other
and it replaces the string with user input
In LINQ you can use Select to project an original sequence into a new sequence.
mtreit
REPL Result: Success
Console Output
Compile: 787.350ms | Execution: 91.401ms | React with ❌ to remove this embed.
If I was starting with a List I probably wouldn't use LINQ but if you want to, that's one way to approach it.
oh alr
what if you wanted to change a and c
Add more logic inside the lambda
You could define a dictionary of replacements too
mtreit
REPL Result: Success
Console Output
Compile: 759.774ms | Execution: 154.762ms | React with ❌ to remove this embed.
Or a switch expression, even better, yea
yeah that's good
thanks guys
why wouldn't you use Linq when its a list?
Performance
could you ellaborate a little more?
LINQ in general is not great for performance so my first inclination is to avoid it if there is a trivial way to achieve the same thing. In most cases it probably doesn't matter much, but I am used to working on projects where performance is very important and the amount of data is large so LINQ is something I tend to avoid.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.