Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串操作-这能做得更好吗?_C#_String - Fatal编程技术网

C# 字符串操作-这能做得更好吗?

C# 字符串操作-这能做得更好吗?,c#,string,C#,String,现在我们正在手动滚动登录,其中一个要求是拥有登录页面 通知用户要登录到哪个模块。现在我唯一要做的就是工作 这样,他们将要登录的URL位于如下查询字符串中: Request.QueryString["ReturnURL"] 具有如下值: ~/moduleFolder/SpecificPage.aspx private static string GetMiddleSegment(string URL) { // you should probably use a library fun

现在我们正在手动滚动登录,其中一个要求是拥有登录页面 通知用户要登录到哪个模块。现在我唯一要做的就是工作 这样,他们将要登录的URL位于如下查询字符串中:

Request.QueryString["ReturnURL"]
具有如下值:

~/moduleFolder/SpecificPage.aspx
private static string GetMiddleSegment(string URL)
{
    // you should probably use a library function for this kind of thing

    int first = URL.IndexOf(@"/");
    int last = URL.LastIndexOf(@"/");
    return URL.Substring(first + 1, last - first - 1); // this is correct, right?
}

private static string SeparateWords(string camelCase)
{
    return Regex.Replace(camelCase, "([a-z])([A-Z])", "$1 $2");
}

private static string Uppercase(string name)
{
    return char.ToUpper(name[0]) + name.Substring(1);
}

// ...

string incomingURL = Request.QueryString["ReturnURL"].ToString();
string nameSegment = GetMiddleSegment(incomingURL);
string displayName = Uppercase(SeparateWords(nameSegment));
lblPortalName.Text = displayName;
var tempName = incomingName.Split('/').Last();
这是我创建的方法,可以用正斜杠将URL拆分, 把那一段分开,把第一个字大写,去掉第一个斜杠, 并将其重新指定给标签以供显示。代码示例如下所示:

string incomingName = Request.QueryString["ReturnURL"].ToString();
int first = incomingName.IndexOf(@"/");
int last = incomingName.LastIndexOf(@"/");
string tempName = incomingName.Substring(first, last - first);
string seperatedName = Regex.Replace(tempName, "([a-z])([A-Z])", "$1 $2");
string upperCased = seperatedName.Replace("/", "");
string portalName = char.ToUpper(upperCased[0]) + upperCased.Substring(1);
lblPortalName.Text = portalName;    

有没有一种更干净或更好的方法来编写此代码,而不必使用这么多不同的新字符串实例

我不会改变它。您最多只能保存几行代码,但事实上,这段代码可能更具可读性,因为这允许在操作的每个步骤中使用字符串的描述性名称


虽然您可能会研究库url函数,而不是实现自己的函数。

是的,更简洁的方法如下:

~/moduleFolder/SpecificPage.aspx
private static string GetMiddleSegment(string URL)
{
    // you should probably use a library function for this kind of thing

    int first = URL.IndexOf(@"/");
    int last = URL.LastIndexOf(@"/");
    return URL.Substring(first + 1, last - first - 1); // this is correct, right?
}

private static string SeparateWords(string camelCase)
{
    return Regex.Replace(camelCase, "([a-z])([A-Z])", "$1 $2");
}

private static string Uppercase(string name)
{
    return char.ToUpper(name[0]) + name.Substring(1);
}

// ...

string incomingURL = Request.QueryString["ReturnURL"].ToString();
string nameSegment = GetMiddleSegment(incomingURL);
string displayName = Uppercase(SeparateWords(nameSegment));
lblPortalName.Text = displayName;
var tempName = incomingName.Split('/').Last();

您会注意到,我的代码不会创建更少的字符串实例。这是因为这里创建的字符串实例的数量与您在处理请求时的性能没有任何关系。

我将编写代码,以获得第一个和第二个/s之间的部分,如下所示:

~/moduleFolder/SpecificPage.aspx
private static string GetMiddleSegment(string URL)
{
    // you should probably use a library function for this kind of thing

    int first = URL.IndexOf(@"/");
    int last = URL.LastIndexOf(@"/");
    return URL.Substring(first + 1, last - first - 1); // this is correct, right?
}

private static string SeparateWords(string camelCase)
{
    return Regex.Replace(camelCase, "([a-z])([A-Z])", "$1 $2");
}

private static string Uppercase(string name)
{
    return char.ToUpper(name[0]) + name.Substring(1);
}

// ...

string incomingURL = Request.QueryString["ReturnURL"].ToString();
string nameSegment = GetMiddleSegment(incomingURL);
string displayName = Uppercase(SeparateWords(nameSegment));
lblPortalName.Text = displayName;
var tempName = incomingName.Split('/').Last();
这是相当简洁,我不难理解。此外,它还省去了你做这些事情的麻烦

string upperCased = seperatedName.Replace("/", "");
因此,最终结果是在不损失可读性的情况下缩短3行

编辑:

简化了OP的注释后的
Split
调用,即只有两个斜杠。
.Last()
调用当然可以替换为
[1]
,尽管我个人更喜欢
.Last()
读取的方式。它比一个幻数更能清楚地表达意图,尤其是如果您不是代码的作者。

基于RedFilter的正则表达式,您可以创建简单的扩展方法,如:

string url = "~/moduleFolder/SpecificPage.aspx";
string moduleFolder = url.Split('/')[1];
string separatedName = Regex.Replace(moduleFolder, "([a-z])([A-Z])", "$1 $2");
string portalName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(separatedName);
private static string UrlModuleName(this string url)
{
    return Regex.Replace(url.Split('/')[1], "([a-z])([A-Z])",
                         "$1 $2").ToTitleCaseInvariant();
}
private static string ToTitleCaseInvariant(this string input)
{
    return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input);
}

这是一个简单的好主意。

可以使用字符串生成器类将其制作成基于库的助手是一个好主意。他的代码获取第一个和最后一个“/”之间的部分,而不一定是第一个和第二个“/”。当然,也许在他的情况下没有什么不同。@mquander:对。但是看起来OP的代码会将斜杠分隔的部分放在一起运行,所以他可能假设只有两个斜杠?var tempName=incomingName.Split('/')[1];在我工作的环境中,我们将只在两个层中工作。根文件夹和辅助文件夹。因此,我们将始终使用斜杠文件夹斜杠页面格式。字符串的名称比我编造的内容更有意义。@克里斯:通过内联
CurrentCulture
内容,使其缩短了一行。