C
C#16mo ago
mattu

❔ 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
Angius
Angius16mo ago
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
mattu
mattuOP16mo ago
.replace()
Angius
Angius16mo ago
Yeah, and why would LINQ need this method? What would you want to replace with it? Because string already does have a replace method
mtreit
mtreit16mo ago
What's your starting point? A list of strings?
mattu
mattuOP16mo ago
yeah like words i mean it's okay I think i know a way to work around something anyway
Angius
Angius16mo ago
And you want to replace every "dog" into "cat" or some such? ["dog", "is", "dog"] into ["cat", "is", "cat"] for example?
mattu
mattuOP16mo ago
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
mtreit
mtreit16mo ago
In LINQ you can use Select to project an original sequence into a new sequence.
MODiX
MODiX16mo ago
mtreit
REPL Result: Success
var strings = new List<string>() { "A", "B", "C" };
var updated = strings.Select(x =>
{
if (x == "A")
{
return "Z";
}

return x;
});

foreach (var s in updated)
{
Console.WriteLine(s);
}
var strings = new List<string>() { "A", "B", "C" };
var updated = strings.Select(x =>
{
if (x == "A")
{
return "Z";
}

return x;
});

foreach (var s in updated)
{
Console.WriteLine(s);
}
Console Output
Z
B
C
Z
B
C
Compile: 787.350ms | Execution: 91.401ms | React with ❌ to remove this embed.
mtreit
mtreit16mo ago
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.
mattu
mattuOP16mo ago
oh alr what if you wanted to change a and c
mtreit
mtreit16mo ago
Add more logic inside the lambda
Angius
Angius16mo ago
You could define a dictionary of replacements too
MODiX
MODiX16mo ago
mtreit
REPL Result: Success
var strings = new List<string>() { "A", "B", "C" };
var updated = strings.Select(x =>
{
return x switch
{
"A" => "Z",
"C" => "Y",
_ => x
};
});

foreach (var s in updated)
{
Console.WriteLine(s);
}
var strings = new List<string>() { "A", "B", "C" };
var updated = strings.Select(x =>
{
return x switch
{
"A" => "Z",
"C" => "Y",
_ => x
};
});

foreach (var s in updated)
{
Console.WriteLine(s);
}
Console Output
Z
B
Y
Z
B
Y
Compile: 759.774ms | Execution: 154.762ms | React with ❌ to remove this embed.
Angius
Angius16mo ago
Or a switch expression, even better, yea
mattu
mattuOP16mo ago
yeah that's good thanks guys
Florian Voß
Florian Voß16mo ago
why wouldn't you use Linq when its a list?
mtreit
mtreit16mo ago
Performance
Florian Voß
Florian Voß16mo ago
could you ellaborate a little more?
mtreit
mtreit16mo ago
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.
Accord
Accord16mo ago
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.
Want results from more Discord servers?
Add your server