Error[8]: Undefined offset: 7, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述例如: thisIsMySample 应该: this_Is_My_Sample 我的代码: System.Text.RegularExpressions.Regex.Replace(input, "([A-Z])", "_$0", System.Text.RegularExpressions.RegexOptions.Compiled); 它工作正常,但如果输入更改为: ThisIsMySamp 例如:
thisIsMySample

应该:

this_Is_My_Sample

我的代码:

System.Text.RegularExpressions.Regex.Replace(input,"([A-Z])","_
ThisIsMySample
",System.Text.RegularExpressions.RegexOptions.Compiled);

它工作正常,但如果输入更改为:

_This_Is_My_Sample

输出将为:

string result = string.Concat(input.Select((x,i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));

如何忽略第一次发生?

解决方法 非正则表达式解决方案
Stopwatch stp = new Stopwatch();stp.Start();for (int i = 0; i < 1000000; i++){    string input = "ThisIsMySample";    string result = System.Text.RegularExpressions.Regex.Replace(input,"(?<=.)([A-Z])",System.Text.RegularExpressions.RegexOptions.Compiled);}stp.Stop();MessageBox.Show(stp.ElapsedMilliseconds.ToString());// Result 2569msStopwatch stp2 = new Stopwatch();stp2.Start();for (int i = 0; i < 1000000; i++){    string input = "ThisIsMySample";    string result = string.Concat(input.Select((x,j) => j > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));}stp2.Stop();MessageBox.Show(stp2.ElapsedMilliseconds.ToString());// Result: 1489ms

似乎也很快:正则表达式:2569ms,C#:1489ms

[+++] 总结

以上是内存溢出为你收集整理的c# – 正则表达式骆驼案以下划线.忽略第一次发生全部内容,希望文章能够帮你解决c# – 正则表达式骆驼案以下划线.忽略第一次发生所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
c# – 正则表达式骆驼案以下划线.忽略第一次发生_C_内存溢出

c# – 正则表达式骆驼案以下划线.忽略第一次发生

c# – 正则表达式骆驼案以下划线.忽略第一次发生,第1张

概述例如: thisIsMySample 应该: this_Is_My_Sample 我的代码: System.Text.RegularExpressions.Regex.Replace(input, "([A-Z])", "_$0", System.Text.RegularExpressions.RegexOptions.Compiled); 它工作正常,但如果输入更改为: ThisIsMySamp 例如:
thisIsMySample

应该:

this_Is_My_Sample

我的代码:

System.Text.RegularExpressions.Regex.Replace(input,"([A-Z])","_
ThisIsMySample
",System.Text.RegularExpressions.RegexOptions.Compiled);

它工作正常,但如果输入更改为:

_This_Is_My_Sample

输出将为:

string result = string.Concat(input.Select((x,i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));

如何忽略第一次发生?

解决方法 非正则表达式解决方案
Stopwatch stp = new Stopwatch();stp.Start();for (int i = 0; i < 1000000; i++){    string input = "ThisIsMySample";    string result = System.Text.RegularExpressions.Regex.Replace(input,"(?<=.)([A-Z])",System.Text.RegularExpressions.RegexOptions.Compiled);}stp.Stop();MessageBox.Show(stp.ElapsedMilliseconds.ToString());// Result 2569msStopwatch stp2 = new Stopwatch();stp2.Start();for (int i = 0; i < 1000000; i++){    string input = "ThisIsMySample";    string result = string.Concat(input.Select((x,j) => j > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));}stp2.Stop();MessageBox.Show(stp2.ElapsedMilliseconds.ToString());// Result: 1489ms

似乎也很快:正则表达式:2569ms,C#:1489ms

总结

以上是内存溢出为你收集整理的c# – 正则表达式骆驼案以下划线.忽略第一次发生全部内容,希望文章能够帮你解决c# – 正则表达式骆驼案以下划线.忽略第一次发生所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1257914.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存