Last active
June 18, 2024 23:14
-
-
Save HorridModz/9fc6eb371aeb2d1d3b2db7ce9f6a6c36 to your computer and use it in GitHub Desktop.
Simple little extension method / wrapper to support Zip() function on strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Collections; | |
using System.Collections.Generic; | |
static class ZipStringExtension | |
{ | |
public static IEnumerable<List<char>> Zip(this string str1, string str2) | |
{ | |
/* Zips two strings together, so that they may be iterated over. | |
* Wrapper for Enumerable.Zip() method; convenient because it converts strings to lists and specifies to return zipped element as an | |
* IEnumerable containing List<char>'s of the two chars for each index | |
* Returns an IEnumerable containing Lists of two chars each. | |
* The function can be called and iterated over like this: | |
* foreach (List<char> chars in Zip(str1, str2) | |
{ | |
char char1 = chars[0]; char char2 = chars[1]; | |
} | |
*/ | |
return str1.ToList().Zip(str2.ToList(), (char1, char2) => new List<char>() { char1, char2 }); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections; | |
using System.Collections.Generic; | |
static class Program { | |
public static IEnumerable<List<char>> Zip(this string str1, string str2) | |
{ | |
/* Zips two strings together, so that they may be iterated over. | |
* Wrapper for Enumerable.Zip() method; convenient because it converts strings to lists and specifies to return zipped element as an | |
* IEnumerable containing List<char>'s of the two chars for each index | |
* Returns an IEnumerable containing Lists of two chars each. | |
* The function can be called and iterated over like this: | |
* foreach (List<char> chars in Zip(str1, str2) | |
{ | |
char char1 = chars[0]; char char2 = chars[1]; | |
} | |
*/ | |
return str1.ToList().Zip(str2.ToList(), (char1, char2) => new List<char>() { char1, char2 }); | |
} | |
static void Main() | |
{ | |
// Demonstrates example usage of the string.Zip() extension method | |
string str1 = "apple"; | |
string str2 = "banana"; | |
List<List<char>> zippedChars = Zip(str1, str2).ToList(); | |
for (int charindex = 0; charindex < zippedChars.Count; charindex++) | |
{ | |
List<char> charsatindex = zippedChars[charindex]; | |
Console.WriteLine($"Chars at index {charindex}: char from str1 is {charsatindex[0]}, char from str2 is {charsatindex[1]}"); | |
} | |
/* Output: | |
Chars at index 0: char from str1 is a, char from str2 is b | |
Chars at index 1: char from str1 is p, char from str2 is a | |
Chars at index 2: char from str1 is p, char from str2 is n | |
Chars at index 3: char from str1 is l, char from str2 is a | |
Chars at index 4: char from str1 is e, char from str2 is n | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment