Common String Methods
var mySeries = new string('*', 10);
var testString = "My String...";
Console.WriteLine(testString.StartsWith("My"));
Console.WriteLine(testString.EndsWith("My"));
Console.WriteLine(testString.EndsWith("My"));
Console.WriteLine(testString.IndexOf("...", StringComparison.InvariantCultureIgnoreCase));
Console.WriteLine(testString.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase));
Console.WriteLine(testString.IndexOfAny(new[] {'S', 'r'}));
Console.WriteLine(testString.LastIndexOfAny(new[] {'y', 'g'}));
Console.WriteLine(testString.PadLeft(20, '+'));
Console.WriteLine(testString.PadRight(20, '+'));
Console.WriteLine(testString.Substring(3, 6));
Console.WriteLine(testString.Remove(5));
Console.WriteLine(testString.Remove(3, 2));
Console.WriteLine(testString.Replace("My", "Your"));
Console.WriteLine(testString.TrimStart());
Console.WriteLine(testString.TrimEnd());
Console.WriteLine(testString.Trim());
var newString1 = testString.Insert(testString.Length, "New Value");
var newString2 = string.Join(" | ", "Value 1", "New Value 2");
var stringArray = newString2.Split(" | ", StringSplitOptions.RemoveEmptyEntries);
foreach (var c in testString)
{
Console.WriteLine(char.IsDigit(c));
Console.WriteLine(char.IsLetter(c));
Console.WriteLine(char.GetNumericValue(c));
Console.WriteLine(char.IsControl(c));
Console.WriteLine(char.IsSurrogate(c));
Console.WriteLine(char.IsUpper(c));
Console.WriteLine(char.IsLower(c));
Console.WriteLine(char.IsPunctuation(c));
Console.WriteLine(char.IsSymbol(c));
Console.WriteLine(char.IsWhiteSpace(c));
Console.WriteLine(char.IsSeparator(c));
}
byte[] bytes = Convert.FromBase64String(textString);
string str = Convert.ToBase64String(bytes);
Best Practices for Using Strings in .NET
String Intern Pool
- The CLR conserves string storage by maintaining a table, called the intern pool.
- This table contains a single reference to each unique literal string declared or created programmatically.
- Consequently, an instance of a literal string with a particular value only exists once in the system.
- If you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.
- The
Intern
method uses the intern pool to search for a string equal to the value of string.
- If such a string exists, its reference in the intern pool is returned.
- If the string does not exist, a reference to string is added to the intern pool, then that reference is returned.
- A character encoding is a system that pairs each character in a supported character set with some value that represents that character.
- A character encoding has two distinct components:
- Encoder: translates a sequence of characters into a sequence of numeric values (bytes).
- Decoder: translates a sequence of bytes into a sequence of characters.
- All character encoding classes in .NET inherit from the
System.Text.Encoding
class.
- It is an abstract class that defines the functionality common to all character encodings.
- To access the individual encoding objects implemented in .NET, do the following:
- Use the static properties of the
Encoding
class.
- It returns objects that represent the standard character encodings (ASCII, UTF-7, UTF-8, UTF-16, and UTF-32).
- Each object uses replacement fallback to handle strings that it cannot encode and bytes that it cannot decode.
- Call the encoding's class constructor.
- Objects for the ASCII, UTF-7, UTF-8, UTF-16, and UTF-32 encodings can be instantiated in this way.
- By default, each object uses replacement fallback, but you can specify that an exception should be thrown instead.
- Call the
Encoding.Encoding(Int32)
constructor and pass it an integer that represents the encoding.
- The standard encoding objects use replacement fallback
- The code page and "double-byte character set" (DBCS) encoding objects use best-fit fallback to handle unsupported strings or bytes.
- Call the
Encoding.GetEncoding
method, which returns any standard, code page, or DBCS encoding available in .NET.
- Overloads let you specify a fallback object for both the encoder and the decoder.
- You compare strings to answer one of two questions:
- Are these two strings equal?
- In what order should these strings be placed when sorting them?
- Those two questions are complicated by factors that affect string comparisons:
- You can choose an ordinal or linguistic comparison.
- You can choose if case matters.
- You can choose culture specific comparisons.
- Linguistic comparisons are culture and platform dependent.
- It offers features for string comparison that are not available in a simple comparison operator.
var areEqual = string.Equals("Equality", "equity", StringComparison.InvariantCultureIgnoreCase);
- It compares two instances of
string
returns an integer that indicates whether the first instance precedes, follows, or appears in the same position in the sort order.
- Negative ⇒ The first instance precedes the second.
- 'some text' ⇒ 'Some Text'
- Zero ⇒ The first instance has the same position in the sort order as the second.
- 'some text' ⇒ 'some text'
- Positive ⇒ The first instance follows the second, or the second value is
null
.