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: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);
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: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());
Source: stackoverflow.com
Related Query
- How to replace a character in string using LINQ
- How to replace a particular string in c# using regex or linq
- How to replace some particular string in a list of type string using linq?
- How to remove characters from a string using LINQ
- string replace using Linq in c#
- How can I sort a string of text followed by a number using LINQ
- using LINQ how can i concatenate string properties from itesm in a collection
- How to split a string using LINQ
- How to reuse a linq expression for 'Where' when using multiple source tables
- How might I complete this example using LINQ and string parsing?
- How to search string not in char array using LinQ
- How can I write the following code more elegantly using LINQ query syntax?
- How can I code an outer join using LINQ and EF6?
- How to read a string with UTF8 coding using linq to entities
- C# .Net 3.5 Code to replace a file extension using LINQ
- How to get data in string array or JSON format using linq to entities?
- how to search string in Linq to SQL using contains
- c# Replace string in a property of a class using linq not working
- How to use LINQ to get last string in list<string> which has character 'P' in second position in string?
- Using LINQ lambda to replace characters from array in string
- How to get mismatch position using Linq or lambda operation on two string array
- How to flatten a multi level XML into a single level XML using c# code LINQ
- How to write this code using the Linq Extension Method-Syntax?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- C# - How to achieve multiple matching from a single string by using Linq and Regex?
- Dynamic Linq using Data Objects. How to convert Int32 to String for purpose of calling String.Contains()
- Find string with most frequency of a character in List of strings using LINQ C#
- How can I return a string using LINQ
- How to simplify the code Using LINQ
- How to search a list of objects for a specific string attribute using LINQ in C#
More Query from same tag
- Get index of object in a list using Linq
- How to use LINQ to group by one field, sort within that grouping, and sum values for that combination?
- How to do select multiple records based on max version using Entity Framework
- How to map complex linq to object
- Select based on child of child with condition
- Using LINQ to select highest earning employee per department
- Regarding FirstOrDefault or SingleOrDefault
- How to get all items of container in cosmos db with dotnet core
- Pivoting data from different tables
- System OutOfMemoryException when generating array of random numbers
- Comparing nullable datetime
- how to use (dynamic)null concept to this query
- Group items in a list based on two properties using LINQ
- How to use Data Binding Linq to SQL to add custom fields
- How to check the attribute value string from linq in c#
- Most popular GroupBy with Linq to Entities
- Datatable updating without using datarow loop
- Can not select information of date in query made before
- Need lambda expression OrderBy with DateTime conversion
- Building an object list with Linq to Xml
- Linq - Take x-amount of distinct items from a list
- Store and Group <Key> based on <Value> of All Dictionary Records Where <Value> Matches X
- Combining join and select statement in LINQ to SQL
- Getting all indexes of matching elements in string array
- Inconsitency error inside Entity Framework , "Collection was modified; enumeration operation may not execute."
- Correct Syntax for Order By with Two Variables?
- Check if two dates completely overlaps a date range
- Is there a way to get the difference between two sets of objects in c#
- How to filter data in a list using multiple conditions and LINQ?
- convert query syntax to Lambda expression