I want to get pairs of first and second letters of strings with the same length (by using LINQ)

Related searches

I have 2 lists of strings

List<string> A = new List<string> { "1QWERTY", "ASDF4B2J", "C0QW", "8D" };
List<string> B = new List<string> { "GJ65SJ2", "KO21L94B", "ABF287KF2", "GPK25" };

And I want to get pairs of first and second letters of strings with the same length (by using LINQ).

Like 1Q: GJ or AS: KO.

So, I wrote this code:

var res = A.Join(
  B,
  a => a.Length,
  b => b.Length,
  (a, b) => new { str = a[0] + a[1] + ": " + b[0] + b[1] }
);

foreach (var e in res)
{
    Console.WriteLine(e.str);
}

But instead of 1Q or AS using a[0] and a[1] I got some numbers. So, result of this request is:

130: GJ
148: KO

But when I use only a[0] or a[1]:

var res = A.Join(B, a => a.Length, b => b.Length, (a, b) => new { str = a[0] + ": " + b[0] + b[1] });

I got a correct symbol, like:

1: GJ
A: KO

So, why does It works like that and how should I change my code to make it work correctly?

This is nothing to do with linq.

a[0] is a char, not a string. Perhaps confusingly, there's no addition operator defined for char: you can't add two chars together to get a string.

However, a char is implicitly convertible to the integral types, and there's an addition operator for ints. So if you do 'A' + 'B', that gets converted to 65 + 66, which is the integer 131.

Then we try and add this to a string. There is an addition operator defined between anything and a string, which works by calling ToString() on the thing that isn't a string, and then concatenating it with the string. So 131 + ": " is the same as 131.ToString() + ": ", which is the string "131: ".

When we come to add on b[0] and b[1], we do this by adding first doing "131: " + b[0]. This follows the same rules about string addition as above, so gets turned into "131: " + b[0].ToString(), which is e.g. "131: " + 'C'.ToString(), which is "131: C". Then we do the same when adding on b[1].

There are several ways to do what you want: you can do a[0].ToString() + a[1] + ": " + ... to force that first char to turn into a string. Or you can do "" + a[0] + ... to achieve the same thing. The easiest thing, though, is not to use string concatenation at all, and instead do $"{a[0]}{a[1]}: {b[0]}{b[1]}".

Another way is to not pick out individual chars, but instead of a.Substring(0, 2) to pull out the first two chars as a string. Then you can write either a.Substring(0, 2) + ": " + b.Substring(0, 2), or $"{a.Substring(0, 2)}: {b.Substring(0, 2)}"`.

Note that none of this is safe if you've got any strings which are 0 or 1 characters long!

Chapter 11. Query expressions and LINQ to Objects, We'll explore two distinct aspects of LINQ: the framework support and the compiler There are times where in order to fetch the first result of a query, you have to We'll use the same idea for almost all of the sample code in this chapter—it's a fairly Suspend your disbelief, and imagine that finding the length of a string is a� When using SequenceEqual to compare two lists or arrays ,the items in the two collections should be the same in the same ordinal.For example if the first list contains the value “A” in the first position then the second list should also contain the value “A” in the first position.

You can simply update the result selector Func in Join method to

(a, b) => new { str = $"{a.Substring(0,2)}:{b.Substring(0,2)}" }

This will get first two letters from a and b strings and concatenate them into one string using string interpolation. Output will be the following

1Q:GJ
AS:KO

Javascript count occurrences in string, Length - 1; // Solution Jul 14, 2017 � Each character in a JavaScript The first thing you need to do is grab a text file in which you have to javascript; Using indexOf() to count occurrences of a letter in a string. We will show you how to do this using two How to count the occurrence of character in a string� Chris Sainty's answer is probably the fastest, but here's the shortest - using Linq as requested:. using System; using System.Collections.Generic; using System.Linq; using System.Text; public static class DictionaryExtension { public static string ToStringFlattened(this Dictionary<string, string> source, string keyValueSeparator="=", char sequenceSeparator='|') { return source == null ?

In your solution instead of using string index use string Substring as follows

var res = A.Join(B,
              a => a.Length,
              b => b.Length,
              (a, b) => new { str = a.Substring(0,2) + ":" + b.Substring(0,2) });

Split String into groups of characters without using delimiters, Length;; // put numbers into pairs, create int array; var mapInfo : int[] = new Is there a way to split into groups of 2,3,4 etc using LINQ? However I really don't understand LINQ (have a hard time reading I tried : declaring i as an int first, then in the Select bracket where i is first used, but those didn't work. Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word; Program to find Smallest and Largest Word in a String; Count substrings with same first and last characters; Recursive solution to count substrings with same first and last characters; Count of distinct substrings of a string using Suffix Array

C# foreach Loop Examples, Step 2: We use foreach to loop through the elements in the array. This example uses a foreach-loop to evaluate a LINQ expression. orderby letter select letter; // Step 3: loop with the foreach keyword. foreach (string value in sorted) { Console . Warning: This foreach-reverse loop will have worse performance than a� Find all even length binary sequences with same sum of first and second half bits; Generate all binary strings of length n with sub-string "01" appearing exactly twice; Length of longest consecutive ones by at most one swap in a Binary String; Generate Binary Strings of length N using Branch and Bound

Reversing a string - two approaches, I didn't believe that the LINQ method could be faster, and I would never In the first approach, the check for an empty string is not necessary. practice to use a StringBuilder to create strings in this way, like so: Length); for (int i = input. expressed as pairs of two surrogates, which when reversed will be� Common characters in n strings Last Updated: 08-02-2019 Given n strings, find the common characters in all the strings. In simple words, find characters that appear in all the strings and display them in alphabetical order or lexicographical order.

The first line of input contains T, the number of test cases. Then follows the description of T test cases. Each test case contains the number N on the first line. The next two lines of the test case contain two N letter strings, A and B respectively. The letters are lowercase engilish letters. Output Print a single line for each test case.

Comments
  • Because you added ": " just before. So, you are addingb[0] and b[1] to a string, not to another char.
  • Thanks, i add " " before a[0] and it works now, but how should I change my code to make it more universal without adding " "?
  • @Roomey Use interpolation or string.Format.
  • I would use string.Format for this kind of thing, and not bother with the + signs.
  • Alternatively, you can use a StringBuilder, which has an Append method overload that accepts individual characters.