Skip to content

DimonSmart/StringTrimmer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StringTrimmer

Don't ever rely on user input! This package offers the ability to trim every string in a class. Sanitizing input data before processing is beneficial.

Note

Please take a look at the Source Genarator based string trimmer. It's faster! https://github.com/DimonSmart/StringTrimmerGenerator

Problem statement

Standard class for describing a user.

public class User
{
   public string Name {get; set;}
   public string Surname {get; set;}
   public string Secondname {get; set;}
   public string UserName {get; set;}
   public string Email {get; set;}
   public string Phone {get; set;}
}

Usage of the User class to add a new User record to the database is typical.

Can you find an error?

Try 
public void CreateUser (User user)
{
   user.Name = user.Name.Trim();
   user.Surname = user.Surname.Trim();
   user.Secondname = user.Secondname.Trim();
   user.Email = user.Email.Trim();
   user.Phone = user.Phone.Trim();
}

Error: UserName Trim command is missing. Due to the tiny difference in UserName, this allows for many users while having only one real user.

  • "John Doe "
  • " John Doe"
  • " John Doe "
  • "John Doe"

Having the same user registered many times irritates the support personnel!

Note: It is usual for users to not merely input a UserName during registration, but to copy it from other sources with different spaces surrounding (and inside) the name.

Magic bullet! (Usage example)

Try 
public void CreateUser (User user)
{
   user.TrimExtraSpaces(); // Just one line
}

About

Trim all string properties in the class at once!

Resources

License

Stars

Watchers

Forks

Contributors

Languages