对字符串应用“掩码”

时间:2012-09-21 12:24:58

标签: c# .net vb.net string

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

说这是FULL字符串和任意掩码

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)

6 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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();

答案 2 :(得分: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
        : '-')));

答案 3 :(得分: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);

答案 4 :(得分:1)

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

var result = new string(strFullWeek.Select((letter, index) 
                         => strWeekMask[index] == '1' ? letter : '-').ToArray());

答案 5 :(得分: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