gpt4 book ai didi

javascript - 将一段js翻译成asp.net(涉及regex)

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:11 25 4
gpt4 key购买 nike

由于性能问题,我正在将我们的一段编程代码从客户端移到服务器端(注意:并非所有代码都在帖子中,只是我遇到问题的部分)

我遇到麻烦的具体部分是别名重写。谁能帮我翻译这部分。

    //1. Force UperCamelCase: "Een lange financiele 443 naam" -> "Een Lange Financiele 443 Naam"
var strTempAlias = this.strName.replace(/\b[a-z]/g, function (match) { return match.toUpperCase() });

//2. Only allow a-zA-Z chars. "Een Lange Financiele 443 Naam" -> "EenLangeFinancieleNaam"
strTempAlias = strTempAlias.replace(/[^a-zA-Z]/g, '');

//3. Until max length 25: progressive remove small characters: "EenLangeFinaNaam"
var intShrinkSize = 10
while (strTempAlias.length > 25 && intShrinkSize > 3) {
var r = new RegExp("([A-Z]+[a-z]{0," + intShrinkSize + "})([a-z]+)", "g")
strTempAlias = strTempAlias.replace(r, function (match, $1, $2) { return $1 });
intShrinkSize -= 2;
}

我已经能够将步骤 1 和 2 转换为 .net 代码,但我不知道如何转换步骤 3。

您可以在下面找到 vb.net 和 c# 中的转换版本(对我来说,回答哪个版本并不重要,我可以对两者进行编程)

VB.NET

        '1. Force UperCamelCase: "Een lange financiele 443 naam" -> "Een Lange Financiele 443 Naam"
Dim strTempAlias As String = StrConv(strAlias, VbStrConv.ProperCase)

'2. Only allow a-zA-Z chars. "Een Lange Financiele 443 Naam" -> "EenLangeFinancieleNaam"
strTempAlias = Regex.Replace(strTempAlias, "[^A-Za-z0-9]+", "")

'3. Until max length 25: progressive remove small characters: "EenLangeFinaNaam"
Dim shrinksize = 10
While strTempAlias.Length > 25 AndAlso shrinksize > 3
'last piece of code to translate
End While

C#

//1. Force UperCamelCase: "Een lange financiele 443 naam" -> "Een Lange Financiele 443 Naam"
string strTempAlias = Strings.StrConv(strAlias, VbStrConv.ProperCase);

//2. Only allow a-zA-Z chars. "Een Lange Financiele 443 Naam" -> "EenLangeFinancieleNaam"
strTempAlias = Regex.Replace(strTempAlias, "[^A-Za-z0-9]+", "");

//3. Until max length 25: progressive remove small characters: "EenLangeFinaNaam"
dynamic shrinksize = 10;
while (strTempAlias.Length > 25 && shrinksize > 3) {
//last piece of code to translate
}

最佳答案

int intShrinkSize = 10;
while (strTempAlias.Length > 25 && shrinksize > 3) {
strTempAlias = Regex.Replace(strTempAlias, "([A-Z]+[a-z]{0," + intShrinkSize + "})([a-z]+)", "$1");
intShrinkSize -= 2;
}

关于javascript - 将一段js翻译成asp.net(涉及regex),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33433019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com