[英]Replace character in string with Uppercase of next in line (Pascal Casing)
I want to remove all underscores from a string with the uppercase of the character following the underscore. 我想从字符串中删除所有下划线,并在下划线后面加上字符的大写字母。 So for example:
_my_string_
becomes: MyString
similarly: my_string
becomes MyString
例如:
_my_string_
变为: MyString
类似: my_string
变为MyString
Is there a simpler way to do it? 有更简单的方法吗? I currently have the following (assuming no input has two consecutive underscores):
我目前有以下(假设没有输入有两个连续的下划线):
StringBuilder sb = new StringBuilder();
int i;
for (i = 0; i < input.Length - 1; i++)
{
if (input[i] == '_')
sb.Append(char.ToUpper(input[++i]));
else if (i == 0)
sb.Append(char.ToUpper(input[i]));
else
sb.Append(input[i]);
}
if (i < input.Length && input[i] != '_')
sb.Append(input[i]);
return sb.ToString();
Now I know this is not totally related, but I thought to run some numbers on the implementations provided in the answers, and here are the results in Milliseconds for each implementation using 1000000 iterations
of the string: "_my_string_121_a_"
: 现在我知道这不是完全相关的,但我想在答案中提供的实现上运行一些数字,这里是使用
1000000 iterations
字符串1000000 iterations
的每个实现的毫秒结果: "_my_string_121_a_"
:
Achilles: 313
Raj: 870
Damian: 7916
Dmitry: 5380
Equalsk: 574
method utilised: 方法:
Stopwatch stp = new Stopwatch();
stp.Start();
for (int i = 0; i < 1000000; i++)
{
sb = Test("_my_string_121_a_");
}
stp.Stop();
long timeConsumed= stp.ElapsedMilliseconds;
In the end I think I'll go with Raj's implementation, because it's just very simple and easy to understand. 最后我想我会选择Raj的实现,因为它非常简单易懂。
This must do it using ToTitleCase using System.Globalization
namespace 这必须使用ToTitleCase使用
System.Globalization
命名空间
static string toCamel(string input)
{
TextInfo info = CultureInfo.CurrentCulture.TextInfo;
input= info.ToTitleCase(input).Replace("_", string.Empty);
return input;
}
Shorter ( regular expressions ), but I doubt if it's better (regular expressions are less readable): 更短( 正则表达式 ),但我怀疑它是否更好(正则表达式不太可读):
string source = "_my_string_123__A_";
// MyString123A
string result = Regex
// _ + lower case Letter -> upper case letter (thanks to Wiktor Stribiżew)
.Replace(source, @"(_+|^)(\p{Ll})?", match => match.Groups[2].Value.ToUpper())
// all the other _ should be just removed
.Replace("_", "");
Loops over each character and converts to uppercase as necessary. 循环遍历每个字符并根据需要转换为大写。
public string GetNewString(string input)
{
var convert = false;
var sb = new StringBuilder();
foreach (var c in input)
{
if (c == '_')
{
convert = true;
continue;
}
if (convert)
{
sb.Append(char.ToUpper(c));
convert = false;
continue;
}
sb.Append(c);
}
return sb.ToString().First().ToString().ToUpper() + sb.ToString().Substring(1);
}
Usage: 用法:
GetNewString("my_string");
GetNewString("___this_is_anewstring_");
GetNewString("___this_is_123new34tring_");
Output: 输出:
MyString
MyString的
ThisIsAnewstringThisIsAnewstring
ThisIs123new34tringThisIs123new34tring
Try with Regex: 试试Regex:
var regex = new Regex("^[a-z]|_[a-z]?");
var result = regex.Replace("my_string_1234", x => x.Value== "_" ? "" : x.Value.Last().ToString().ToUpper());
Tested with: 经测试:
my_string -> MyString
_my_string -> MyString
_my_string_ -> MyString
You need convert snake case to camel case, You can use this code it's working for me 你需要将蛇盒转换成驼峰盒,你可以使用这个代码,它对我有用
var x ="_my_string_".Split(new[] {"_"}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => char.ToUpperInvariant(s[0]) + s.Substring(1, s.Length - 1))
.Aggregate(string.Empty, (s1, s2) => s1 + s2);
x = MyString x = MyString
static string toCamel(string input)
{
StringBuilder sb = new StringBuilder();
int i;
for (i = 0; i < input.Length; i++)
{
if ((i == 0) || (i > 0 && input[i - 1] == '_'))
sb.Append(char.ToUpper(input[i]));
else
sb.Append(char.ToLower(input[i]));
}
return sb.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.