[英]How to lowercase a string except for first character with C#
How do convert a string to lowercase except for the first character? 除第一个字符外,如何将字符串转换为小写? Can this be completed with LINQ? 这可以用LINQ完成吗?
Thanks 谢谢
If you only have one word in the string, you can use TextInfo.ToTitleCase
. 如果字符串中只有一个单词,则可以使用TextInfo.ToTitleCase
。 No need to use Linq. 无需使用Linq。
This will convert any string to title case, so, "hello world" and "HELLO WORLD" would both be converted to "Hello World". 这会将任何字符串转换为标题大小写,因此,“hello world”和“HELLO WORLD”都将转换为“Hello World”。
To achieve exectly what you asked (convert all characters to lower, except the first one), you can do the following: 为了实现exectly你问(转换所有字符时,除了第一个),你可以做到以下几点:
string mostLower = myString.Substring(0, 1) + myString.Substring(1).ToLower();
This can be done with simple string operations: 这可以通过简单的字符串操作完成:
s = s.Substring(0, 1) + s.Substring(1).ToLower();
Note that this does exactly what you asked for, ie it converts all characters to lower case except the first one that is left unchanged. 请注意,这完全符合您的要求,即它将所有字符转换为小写,但第一个字符保持不变。
If you instead also want to change the first character to upper case, you would do: 如果您还想将第一个字符更改为大写字母,则可以执行以下操作:
s = s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
Note that this code assumes that there is at least two characters in the strings. 请注意,此代码假定字符串中至少有两个字符。 If there is a possibility that it's shorter, you should of course test for that first. 如果有可能它更短,你当然应该首先测试它。
String newString = new String(str.Select((ch, index) => (index == 0) ? ch : Char.ToLower(ch)).ToArray());
Use namespace: using System.Globalization;
使用命名空间: using System.Globalization;
... ...
string value = CultureInfo.CurrentCulture.TextInfo.ToTitleCase("hello");
EDIT 编辑
This code work only if its single word .For convert all character into lower except first letter check Guffa Answer. 此代码仅在其单个单词时起作用。对于将所有字符转换为较低的除了第一个字母检查Guffa Answer。
string value = myString.Substring(0, 1) + myString.Substring(1).ToLower();
Not sure you can do it in linq here is a non-linq approach: 不确定你能在linq中做到这里是一个非linq方法:
public static string FirstCap(string value)
{
string result = String.Empty;
if(!String.IsNullOrEmpty(value))
{
if(value.Length == 1)
{
result = value.ToUpper();
}
else
{
result = value.Substring(0,1).ToString().ToUpper() + value.Substring(1).ToLower();
}
}
return result;
}
based on guffa's example above (slightly amended). 基于上面的guffa示例(稍作修改)。 you could convert that to an extension method (please pardon the badly named method :)): 你可以将它转换为扩展方法(请原谅这个命名错误的方法:)):
public static string UpperFirst(this string source)
{
return source.ToLower().Remove(0, 1)
.Insert(0, source.Substring(0, 1).ToUpper());
}
usage: 用法:
var myNewString = myOldString.UpperFirst();
// or simply referenced as myOldString.UpperFirst() where required
cheers guffa 欢呼guffa
var initialString = "Hello hOW r u?";
var res = string.Concat(initialString..ToUpper().Substring(0, 1), initialString.ToLower().Substring(1));
You can use an extension method: 您可以使用扩展方法:
static class StringExtensions
{
public static string ToLowerFirst(this string text)
=> !string.IsNullOrEmpty(text)
? $"{text.Substring(0, 1).ToLower()}{text.Substring(1)}"
: text;
}
Unit tests as well (using FluentAssertions and Microsoft UnitTesting): 单元测试(使用FluentAssertions和Microsoft UnitTesting):
[TestClass]
public class StringExtensionsTests
{
[TestMethod]
public void ToLowerFirst_ShouldReturnCorrectValue()
=> "ABCD"
.ToLowerFirst()
.Should()
.Be("aBCD");
[TestMethod]
public void ToLowerFirst_WhenStringIsEmpty_ShouldReturnCorrectValue()
=> string.Empty
.ToLowerFirst()
.Should()
.Be(string.Empty);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.