14

我有一个标志枚举 (int) 掩码,我需要将其转换为表示星期几的字符串。

说这是完整的字符串和任意掩码

strFullWeek = "MTWtFSs"
strWeekMask = "0100110"
-----------------------
strResult   = "-T--FS-"

您建议以什么方式从整周和掩码字符串中获取 strResult?

更新

这是我的“整个上下文”(VB.NET)

<Flags()> Public Enum Week
  Monday = 1
  Tuesday = 2
  Wednesday = 4
  Thursday = 8
  Friday = 16
  Saturday = 32
  Sunday = 64
End Enum

Dim mondayOrSunday = Week.Monday Or Week.Sunday
Dim strDays = "MTWtFSs"

Dim strMondayOrSundayBinary = Convert.ToString(
  mondayOrSunday, 2).PadRight(7, CChar("0"))

Dim charMondayOrSunday = strDays.Zip(
  strMondayOrSundayBinary,
  Function(day, mask) If(mask = CChar("1"), day, CChar("-"))).ToArray()

Dim strMondayOrSunday = New String(charMondayOrSunday)

Console.WriteLine("{0} I see as {1}",
                  mondayOrSunday,
                  strMondayOrSunday)
4

6 回答 6

20

有一种相当简洁的 LINQ 方式:

var chars = strFullWeek.Zip(strWeekMask, (day, mask) => mask == '1' ? day : '-'))
                       .ToArray();
string text = new string(chars);

它不会非常有效,但它可能已经足够好了......

编辑:好的,假设您对枚举版本的 C# 代码感到满意

// TODO: Rename "Week" to comply with .NET naming conventions
Week mask = Week.Monday | Week.Sunday;
var chars = strFullWeek.Select((value, index) => (int) mask & (1 << index) != 0) 
                                                 ? value : '-')
                       .ToArray();
string text = new string(chars);
于 2012-09-21T12:27:19.733 回答
5

我的快速解决方案使用StringBuilder. 它允许您就地操作字符串。但是,除非您将此代码调用一百万次,否则与 LINQ 解决方案相比,您不会注意到速度上有任何差异。

string strFullWeek = "MTWtFSs";
string strWeekMask = "0100110";

var sb = new StringBuilder(strFullWeek);
for (int i = 0; i < strWeekMask.Length; i++) {
    if (strWeekMask[i] == '0') {
        sb[i] = '-';
    }
}
string result = sb.ToString();
于 2012-09-21T12:46:58.740 回答
3

干得好:

var strFullWeek = "MTWtFSs";
var strWeekMask = "0100110";

var strFullWeekArray = strFullWeek.ToList();
var strWeekMaskArray = strWeekMask.ToList();

Console.WriteLine(string.Join(
    string.Empty,
    strFullWeek.Select(c => (strWeekMaskArray[strFullWeekArray.IndexOf(c)] == '1')
        ? c
        : '-')));
于 2012-09-21T12:36:04.260 回答
2

如果您正在寻找可读性,这是另一种选择:

string fullWeek = "MTWtFSs";
string weekMask = "0100110";

const char blankChar = '-';

int totalChars = fullWeek.Length;

StringBuilder result = new StringBuilder();

for (int index = 0; index < totalChars; index++)
{
    if (weekMask[index] == '1')
    {
        result.Append(fullWeek[index]);
    }
    else
    {
        result.Append(blankChar);
    }
}

Console.WriteLine(result);
于 2012-09-21T12:40:16.510 回答
1

我参加晚会迟到了;但我想试试。这是我不太酷的 LINQ 方法:

var result = new string(strFullWeek.Select((letter, index) 
                         => strWeekMask[index] == '1' ? letter : '-').ToArray());
于 2012-09-21T12:58:22.443 回答
1

如果你想直接使用枚举,你可以这样做:

[Flags()]
enum Week
{
    Monday = 1,
    Tuesday = 2,
    Wednesday = 4,
    Thursday = 8,
    Friday = 16,
    Saturday = 32,
    Sunday = 64
}

const string strFullWeek = "MTWtFSs";

string GetMaskedWeek(Week days)
{
    char[] result = new char[strFullWeek.Length];
    for (int i = 0; i < strFullWeek.Length; i++)
    {
        if ((((int)days >> i) & 1) != 0)
            result[i] = strFullWeek.ToCharArray(i, 1)[0];
        else
            result[i] = '-';
    }
    return new string(result);
}

或者,在 VB.NET 中:

<Flags()> Public Enum Week
    Monday = 1
    Tuesday = 2
    Wednesday = 4
    Thursday = 8
    Friday = 16
    Saturday = 32
    Sunday = 64
End Enum

Dim strFullWeek As String = "MTWtFSs"

Private Function GetMaskedWeek(ByVal days As Week) As String
    Dim result(strFullWeek.Length) As Char
    For i As Integer = 0 To strFullWeek.Length - 1
        If ((days >> i) And 1) <> 0 Then
            result(i) = strFullWeek.ToCharArray(i, 1)(0)
        Else
            result(i) = "-"c
        End If
    Next
    Return New String(result)
End Function
于 2012-09-21T13:07:02.807 回答