score:9
For your interviewer
string output = new String(input.Select(ch => ch == '2' ? '0' : ch).ToArray());
I think intend was to see if you understand that string could be treated as sequence of characters. Interviews often have questions not related to real-life programming. I personally hate questions about inheritance tree with new
modifiers.
score:12
Why you need to do it with LINQ, simple string.Replace should do the trick.
string str = "21212121".Replace("2","0");
EDIT: If you have to use LINQ then may be something like:
string newStr = new string(str.Select(r => (r == '2' ? '0' : r)).ToArray());
score:1
For changing a single character you can use the overload of Replace
that takes char
arguments:
string s = "21212121";
s = s.Replace('2', '0');
This is likely to be slightly more efficient than the overload that accept strings.
If you absolutely have to use LINQ for some reason, then you can do this:
s = new string(s.Select(c => c == '2' ? '0' : c).ToArray());
But you shouldn't do this in production code. It's harder to read and less efficient than string.Replace
.
score:0
string strInput = "21212121";
char from = '2';
char to = '0';
var strOutput = new string(strInput.Select(c => c.Equals(from) ? to : c)
.ToArray());
Console.WriteLine(strOutput);
Source: stackoverflow.com
Related Articles
- How to replace a character in string using LINQ
- string replace using Linq in c#
- C# .Net 3.5 Code to replace a file extension using LINQ
- c# Replace string in a property of a class using linq not working
- Using LINQ lambda to replace characters from array in string
- Find string with most frequency of a character in List of strings using LINQ C#
- How to replace a particular string in c# using regex or linq
- Adding a character at the beginning of a string if it does not exist using LINQ
- Replace all values in IQueryable with string based on results using LINQ
- Convert string[] to int[] in one line of code using LINQ
- C# - code to order by a property using the property name as a string
- Using Linq to return a Comma separated string
- Convert string to int array using LINQ
- Check if a string has at least one number in it using LINQ
- Using LINQ to convert a list to a CSV string
- Collection to string using linq
- Split string into list of N-length strings using LINQ
- How to replace some particular string in a list of type string using linq?
- Using LINQ to parse the numbers from a string
- Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression
- How to remove characters from a string using LINQ
- How can I sort a string of text followed by a number using LINQ
- Concatenate a constant string to each item in a List<string> using LINQ
- Split a separated string into hierarchy using c# and linq
- using LINQ how can i concatenate string properties from itesm in a collection
- Convert string array to custom object list using linq
- convert comma separated string to list using linq
- Extract data from a XML string using linq vs xmlDocument
- How to split a string using LINQ
- Left outer join using LINQ -- understanding the code
- Linq to get distinct subdirectories from list of paths
- How to select record from select new clause in linq
- Get Average of column using group by
- How to make from two methods one method in a repository
- Linq query for Where on the Joined table without needing join
- asp.net mvc c# listing categories recursively
- How to use JSON.Net to read JSON and output HTML?
- Filter DataSet by email address with LINQ query not working
- Dynamic Where Linq C#
- LINQ Join On Multiple Columns With Different Data Types
- Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
- IQueryable command timeout
- IEnumerable Extension Methods on an Enum
- Using Linq for ObjectDataSource: How to transform datetime using ToShortTimeString?
- Linq query with Groupby Select and foreach loop
- Linq result data binding - How to change the column header?
- Can all 'for' loops be replaced with a LINQ statement?
- EF Core rewrite the query in a form that can be translated after upgrading
- How to use join to make this queries cleaner?
- How can I select all in linq to sql