1: /// <summary>
2: /// Provides extension methods for <see cref="string"/>
3: /// </summary>
4: public static class StringExtensions {
5: /// <summary>
6: /// Calls ToString on the given object if <paramref name="obj"/> is not null; otherwise,
7: /// <see cref="string.Empty"/> is returned.
8: /// </summary>
9: /// <param name="obj">The obj.</param>
10: /// <returns>The result of <see cref="object.ToString()"/> invoked on <paramref name="obj"/> if
11: /// <paramref name=" obj"/> is not null; otherwise, <see cref="string.Empty"/></returns>
12: public static string ToSafeString<T>( this T obj ) {
13: if ( typeof( T ).IsValueType ) {
14: return obj.ToString();
15: }
16: return Equals( obj, null ) ? string.Empty : obj.ToString();
17: }
18:
19: /// <summary>
20: /// Formats a string using the given format and a value.
21: /// </summary>
22: /// <param name="format">The format.</param>
23: /// <param name="value">The value.</param>
24: /// <returns>The formatted string</returns>
25: public static string Formatted<T>( this string format, T value )
26: where T : struct {
27: return string.Format( format, value.ToSafeString() );
28: }
29:
30: /// <summary>
31: /// Formats a string using the given format and the given values
32: /// </summary>
33: /// <typeparam name="T1">The type of the first argument.</typeparam>
34: /// <typeparam name="T2">The type of the second argument.</typeparam>
35: /// <param name="format">The format.</param>
36: /// <param name="value1">The first value.</param>
37: /// <param name="value2">The second value.</param>
38: /// <returns>The formatted string</returns>
39: public static string Formatted<T1, T2>( this string format, T1 value1, T2 value2 )
40: where T1 : struct
41: where T2 : struct {
42: return string.Format( format, value1.ToSafeString(), value2.ToSafeString() );
43: }
44:
45: /// <summary>
46: /// Formats a string using the given format and the given values
47: /// </summary>
48: /// <typeparam name="T1">The type of the first argument.</typeparam>
49: /// <typeparam name="T2">The type of the second argument.</typeparam>
50: /// <typeparam name="T3">The type of the third argument.</typeparam>
51: /// <param name="format">The format.</param>
52: /// <param name="value1">The first value.</param>
53: /// <param name="value2">The second value.</param>
54: /// <param name="value3">The third value.</param>
55: /// <returns>The formatted string</returns>
56: public static string Formatted<T1, T2, T3>( this string format, T1 value1, T2 value2, T3 value3 )
57: where T1 : struct
58: where T2 : struct
59: where T3 : struct {
60: return string.Format( format, value1.ToSafeString(), value2.ToSafeString(), value3.ToSafeString() );
61: }
62:
63: /// <summary>
64: /// Formats a string using the given format and the given values.
65: /// </summary>
66: /// <param name="format">The format.</param>
67: /// <param name="values">The valuess.</param>
68: /// <returns>The formatted string</returns>
69: public static string Formatted( this string format, object[] values ) {
70: return string.Format( format, values );
71: }
72: }