web analytics

Working with Strings in C#

Options
@2017-06-21 14:56:26

String.TrimEnd(Char[])

The following example demonstrates how you can use the TrimEnd(Char[]) method to trim white space or punctuation marks from the end of a string.

using System;

public class TrimEnd
{
   public static void Main()
   {
      string sentence = "The dog had a bone, a ball, and other toys.";
      char[] charsToTrim = {',', '.', ' '};
      string[] words = sentence.Split();
      foreach (string word in words)
         Console.WriteLine(word.TrimEnd(charsToTrim));
   }
}
// The example displays the following output:
//       The
//       dog
//       had
//       a
//       bone
//       a
//       ball
//       and
//       other
//       toys
@2017-07-11 09:32:53

Determine if all characters in a string are the same

I have a situation where I need to filter out any string line in a tex file where all the characters are the same. The follow C# code can be used to determine if all the characters are the same:

return (ssn.Distinct().Count() == 1)

@2017-08-14 11:11:05

Convert a String to Title Case

The String class does not include a method that converts a string to title case. The ToTitleCase method resides in the TextInfo class, which is a member of the System.Globalization namespace. Unlike the ToUpper and ToLower methods of the String class, the ToTitleCase method is not a static method and requires an instance of the class.

When you use the TextInfo class, you must specify cultural information. In most situations, you can default to the culture that is currently in use. Culture information is a property of the thread on which the code is running. To obtain the culture information, you must gain access to the current thread and retrieve the CurrentCulture property from that thread. Once you accomplish this, you can create the TextInfo object. For example:
 

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;

TextInfo textInfo = cultureInfo.TextInfo;

The TextInfo class also includes the ToUpper and ToLower methods. Use these methods of TextInfo if you need to specify culture options.

Console.WriteLine(textInfo.ToTitleCase(title));

Console.WriteLine(textInfo.ToLower(title));

Console.WriteLine(textInfo.ToUpper(title));

If you need to create or manipulate strings that have specific culture settings, you can use one of the overloaded constructors of the TextInfo class to create strings with any of the available culture options.

using System;
using System.Globalization;
using System.Threading;

namespace csConsole
{

    public class MyClass
    {
        public static void Main()
        {
          string title="this is my converted string";
          Console.WriteLine("String Class");
          Console.WriteLine("------------");

          //Convert string to uppercase.
          Console.WriteLine(title.ToUpper());
          //Convert string to lowercase.
          Console.WriteLine(title.ToLower());

          Console.WriteLine();
          Console.WriteLine("TextInfo Class");
          Console.WriteLine("--------------");

          //Get the culture property of the thread.
          CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
          //Create TextInfo object.
          TextInfo textInfo = cultureInfo.TextInfo;
                   
          //Convert to uppercase.
          Console.WriteLine(textInfo.ToUpper(title));
          //Convert to lowercase.
          Console.WriteLine(textInfo.ToLower(title));
          //Convert to title case.
          Console.WriteLine(textInfo.ToTitleCase(title));
        }
    }
}

Note on the InvariantCulture Property

When you use the Globalization namespace to convert data, if you store the converted data instead of displaying it for the user, you may want to use the InvariantCulture property of CultureInfo class.

InvariantCulture is neither a neutral nor a specific culture; it is a culture that is culture-insensitive. If you use InvariantCulture when you store data, the data is stored in a consistent manner, regardless of any specific user or cultural system settings that may be in effect. For more information, refer to the References section.

The preceding code sample uses the CultureInfo properties of the current thread:

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;

To use InvariantCulture in the same scenario, use the following code:

CultureInfo cultureInfo = CultureInfo.InvariantCulture;

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com