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
- Extracting direction changes from a list of coordinates
- Convert from IQueryble<T> to T
- C# - LINQ to unflatten items into a hierarchical collection
- Converting linq-to-xml query from vb to c#
- Can anyone help me in completing this generic method to update from one list to another list?
- Select items in List that match an interface
- How to use Linq to select items from a anonymous IEnumerable collection
- Speed up LINQ inserts
- Linq Error : Invalid 'where' condition. An entity member is invoking an invalid property or method
- Slow AsQueryable() and universal method for IQueryable and IEnumerable
- Query to entity framework 7 using linq and related entities
- how to get the value using linq query C#
- How to get differences that caused Except to add an IEnumerable?
- How to use Convert.Int32() method in LINQ to Entities Join?
- Linq to Entities add Where clause for EXISTS lookup in another table
- SQL Server query returns OLD value, until restarting application
- Multiple optional checks with Where query (to specify file search options)
- condition in include in linq to entities
- how to order asc/dsc with lambda or linq
- How can I implement Query Interception in a LINQ to Entities query? (c#)
- Linq Select Users which have the same cars
- LINQ - Comparing dictionaries of lists to find new and changed objects
- How to pass func expression in LINQ where clause?
- Problem with checking an original database record with an edited one
- LINQ query, selecting latest value with group by - Unsupported method
- Apply function to all elements of collection through LINQ
- dilemma on the use of inheritance in EF code-first
- Select Foreign key data in EF
- Does Entity Framework perform lambda expressions before or after it fetches the data?
- Sort a splitted array string with Linq?