Question: I am using the following to convert the first character of a string to upper and add a period at the end:

```
public static string toUpper (string mytext)
{
    if (string.IsNullOrEmpty(mytext) || mytext == "") return mytext;

    mytext = mytext.Trim();

    if (mytext.EndsWith("."))
    {
      mytext = mytext.First().ToString(CultureInfo.InvariantCulture).ToUpper()
             + string.Join("", mytext.Skip(1))
             ;
    }
    else
    {
      mytext = mytext.First().ToString(CultureInfo.InvariantCulture).ToUpper()
             + string.Join("", mytext.Skip(1))
             + "."
             ;
    }
    return mytext;
}

```

I get an error Sequence contains no elements if mytext = "" at this line:

```
 mytext = mytext.First().ToString(CultureInfo.InvariantCulture).ToUpper()
        + string.Join("", mytext.Skip(1))
        + "."
        ;

```

Would appreciate your suggestions.

Answer: 
Seems way more complicated than it has any right to be.

I'd go for elegance and use a regular expression:

```
public static string toUpper( string s )
{
  string value = null ;
  if ( s != null )
  {
    // match length 1: it's the first first character: upper-case it
    // match length 0: the last character isn't a period. append it
    value = rx.Replace( s.Trim() , m => m.Length == 1 ? m.Value.ToUpper( CultureInfo.InvariantCulture ) : "." ) ;
  }
  return value ;
}
static readonly Regex rx = new Regex( @"(^.)|($(?<=[^\.]" ) ;

```

But you could use a string builder...

```
public static string toUpper( string s )
{
  if ( s == null ) return null ;

  StringBuilder sb = new StringBuilder( s.Trim() ) ;
  if ( sb.Length > 0 )
  {
    sb[0] = char.ToUpper(sb[0] , CultureInfo.InvariantCulture ) ;
    if ( sb[ sb.Length-1] != '.' ) sb.Append('.') ;
  }
  value = sb.ToString() ;
  return value ;
}

```
use firstordefault() instead of first(). first throwing an exception in case there are no elements