Issue in capitalizing first letter of first name and last name
我想把名字、中间名和姓氏的第一个字母大写。我正在尝试使用System.Globalization.textinfo.toitlecase(newuser.firstname),但在Visual Studio中遇到一个错误,它说"非静态字段、方法或属性需要对象引用"。帮助我解决这个问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public static UserAccount CreateUser(string firstName, string middleName, string lastName, string nameSuffix, int yearOfBirth, int? monthOfBirth, int? dayOfBirth, string email, string password, UserRole roles, bool tosAccepted = false) { var newUser = new UserAccount { CreationDate = DateTime.Now, ActivationCode = Guid.NewGuid(), FirstName = firstName, MiddleName = middleName, LastName = lastName, NameSuffix = nameSuffix, YearOfBirth = yearOfBirth, MonthOfBirth = monthOfBirth, DayOfBirth = dayOfBirth, Email = email, UserRoles = roles, ToSAccepted = tosAccepted }; string newUsers= System.Globalization.TextInfo.ToTitleCase(newUser.FirstName); newUser.SetPassword(password); return newUser; } |
1 | var textInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo; |
或者来自这样的特定文化:
1 |
号
另外,它返回一个新的字符串,而不是修改您传入的字符串。尝试类似的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public static UserAccount CreateUser(string firstName, string middleName, string lastName, string nameSuffix, int yearOfBirth, int? monthOfBirth, int? dayOfBirth, string email, string password, UserRole roles, bool tosAccepted = false) { var textInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo; var newUser = new UserAccount { CreationDate = DateTime.Now, ActivationCode = Guid.NewGuid(), FirstName = textInfo.ToTitleCase(firstName), MiddleName = middleName, LastName = textInfo.ToTitleCase(lastName), NameSuffix = nameSuffix, YearOfBirth = yearOfBirth, MonthOfBirth = monthOfBirth, DayOfBirth = dayOfBirth, Email = email, UserRoles = roles, ToSAccepted = tosAccepted }; newUser.SetPassword(password); return newUser; } |
您可以调用
1 | CultureInfo.CurrentCulture.TextInfo.ToTitleCase(lastName) |
。