比较两个逗号分隔的字符串是否匹配的最短代码是什么?
下面名为 UserCanAccessThisPage 的方法基于以下逻辑:每个用户和每个页面都有一个组列表。 如果其中任何一个匹配,则用户可以访问该页面。
下面的代码实现了我想要的功能,但是我的解决方案非常 C# 1(或 C# 2,至少我没有使用 ArrayList)。
任何人都可以重构它,使其更直接,例如使用 lambda 来消除这两种方法吗? 我只是无法获得执行此操作的语法。
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestCompare2343
{
class Program
{
static void Main(string[] args)
{
string anonymousUserAccessGroups = "loggedOutUsers";
string normalUserAccessGroups = "loggedInUsers, members";
string developerUserAccessGroups = "loggedInUsers, members, administrators, developers";
string loginPageAccessGroups = "loggedOutUsers";
string logoutPageAccessGroups = "loggedInUsers";
string memberInfoPageAccessGroups = "members";
string devPageAccessGroups = "developers";
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
Console.ReadLine();
}
}
public class StringHelpers
{
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups)
{
List<string> userAccessGroupsList = StringHelpers.SplitAndTrimCommaDelimitedString(userAccessGroups);
List<string> pageItemAccessGroupList = StringHelpers.SplitAndTrimCommaDelimitedString(pageItemAccessGroups);
foreach (string userAccessGroup in userAccessGroupsList)
{
foreach (string pageItemAccessGroup in pageItemAccessGroupList)
{
if (userAccessGroup == pageItemAccessGroup)
return true;
}
}
return false;
}
public static List<string> SplitAndTrimCommaDelimitedString(string line)
{
List<string> piecesWithSpaces = line.Split(',').ToList<string>();
List<string> piecesWithoutSpaces = new List<string>();
foreach (string pieceWithSpace in piecesWithSpaces)
{
piecesWithoutSpaces.Add(pieceWithSpace.Trim());
}
return piecesWithoutSpaces;
}
}
}
答案:
Fredrik 有最简洁的代码来解决上面的原始任务:
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups)
{
return userAccessGroups
.Split(',')
.Select(s => s.Trim())
.Contains(pageItemAccessGroups);
}
我使用的代码:
但是 Shaul 正确地假设 PageItems 也可以有多个条目,例如“成员,客人”,所以我实际上使用了 Shaul 的代码:
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups) {
List<string> userAccessGroupsList = StringHelpers.SplitAndTrimCommaDelimitedString(userAccessGroups);
List<string> pageItemAccessGroupList = StringHelpers.SplitAndTrimCommaDelimitedString(pageItemAccessGroups);
return userAccessGroupsList.Any(userAccessGroup => pageItemAccessGroupList.Any(pageItemAccessGroup => userAccessGroup == pageItemAccessGroup));
}
public static List<string> SplitAndTrimCommaDelimitedString(string line) {
return line.Split(',').Select(s => s.Trim()).ToList();
}
The method below called UserCanAccessThisPage is based on the following logic: each user and each page has a list of groups. If any of these match, the user has access to the page.
The code below does what I want, but my solution is very C# 1 (or C# 2, at least I didn't use ArrayList).
Can anyone refactor this so it is more straight-forward, e.g. using lambdas to do away with the two methods? I just can't get the syntax to do it.
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestCompare2343
{
class Program
{
static void Main(string[] args)
{
string anonymousUserAccessGroups = "loggedOutUsers";
string normalUserAccessGroups = "loggedInUsers, members";
string developerUserAccessGroups = "loggedInUsers, members, administrators, developers";
string loginPageAccessGroups = "loggedOutUsers";
string logoutPageAccessGroups = "loggedInUsers";
string memberInfoPageAccessGroups = "members";
string devPageAccessGroups = "developers";
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
Console.ReadLine();
}
}
public class StringHelpers
{
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups)
{
List<string> userAccessGroupsList = StringHelpers.SplitAndTrimCommaDelimitedString(userAccessGroups);
List<string> pageItemAccessGroupList = StringHelpers.SplitAndTrimCommaDelimitedString(pageItemAccessGroups);
foreach (string userAccessGroup in userAccessGroupsList)
{
foreach (string pageItemAccessGroup in pageItemAccessGroupList)
{
if (userAccessGroup == pageItemAccessGroup)
return true;
}
}
return false;
}
public static List<string> SplitAndTrimCommaDelimitedString(string line)
{
List<string> piecesWithSpaces = line.Split(',').ToList<string>();
List<string> piecesWithoutSpaces = new List<string>();
foreach (string pieceWithSpace in piecesWithSpaces)
{
piecesWithoutSpaces.Add(pieceWithSpace.Trim());
}
return piecesWithoutSpaces;
}
}
}
Answer:
Fredrik had the most concise code that solved the original task above:
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups)
{
return userAccessGroups
.Split(',')
.Select(s => s.Trim())
.Contains(pageItemAccessGroups);
}
The code I used:
But Shaul was correct in assuming that the PageItems can also have multiple entries, e.g. "members,guests", and so I actually used Shaul's code:
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups) {
List<string> userAccessGroupsList = StringHelpers.SplitAndTrimCommaDelimitedString(userAccessGroups);
List<string> pageItemAccessGroupList = StringHelpers.SplitAndTrimCommaDelimitedString(pageItemAccessGroups);
return userAccessGroupsList.Any(userAccessGroup => pageItemAccessGroupList.Any(pageItemAccessGroup => userAccessGroup == pageItemAccessGroup));
}
public static List<string> SplitAndTrimCommaDelimitedString(string line) {
return line.Split(',').Select(s => s.Trim()).ToList();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,你说你想要紧凑型......!
:)
OK, you said you wanted compact...!
:)
StringHelpers 可能如下所示:
StringHelpers could look like this:
一旦您拥有两个
IEnumerable
,您就可以使用Intersect
函数:这是为了您需要页面上用户的完整权限列表。
不过,我会采用 Shaul 的示例:
Any
函数速度更快,它应该在第一个匹配处停止。Once you have the two
IEnumerable<string>
s, you can use theIntersect
function:This is in case you need the full list of permissions for the user on the page.
However, I'd go with Shaul's example: the
Any
function is faster, it should stop at the first match.像这样的东西(抱歉没有IDE)
我真的没有看到它有两个方法有什么大问题,因为功能很好地分离,但我想你可以将代码从第二个内联方法移动到第一个方法,如果你真的想要。
Something like (sorry no IDE to hand)
I really don't see a big problem with it having two methods, since the functionality is nicely separated, but I guess you could move the code from the second method inline to the first if you really want to.