My aim is to “Sanitize a string”.
The class should do:
- trim an input
- make the first letter upper case.
Could you please tell me:
- Is there a way to better code it?
-
Would it make sense to use a PARAMETER for a method like: CapitalizeFirstLetterTrim(string x)
-
when I initiate an object I need write a lot of code like below, any other way to make it shorter?
UserInputSanitizer myInput = new UserInputSanitizer(); myInput.Input = " ciao world"; string ouput = myInput.CapitalizeFirstLetterTrim();
-
Useful resource http://msdn.microsoft.com/en-us/library/bb311042.aspx
———– CLASS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebProject.Core.Utilities
{
public class UserInputSanitizer
{
// Backing variables
private string _input;
// Properties
public string Input
{
set { _input = value; }
}
private string _output;
// Backing variables
// Properties
public string Output
{
get { return _output; }
}
public string CapitalizeFirstLetterTrim()
{
// Trim
_input.Trim();
// Make First letter UpperCase and the rest levae lower case
_output = _input.Substring(0, 1).ToUpper() + _input.Substring(1);
return Output;
}
}
}
I would use an extension method for the string class:
Then you can use: