Return to Snippet

Revision: 27581
at July 13, 2010 06:20 by pckujawa


Updated Code
/// <summary>
/// Determines whether the argument string can be represented with the ASCII (<see cref="Encoding.ASCII"/>) encoding.
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>
/// <c>true</c> if the specified value is ASCII; otherwise, <c>false</c>.
/// </returns>
public static bool IsASCII(this string value)
{
    // ASCII encoding replaces non-ascii with question marks, so we use UTF8 to see if multi-byte sequences are there
    return Encoding.UTF8.GetByteCount(value) == value.Length;
}

[TestMethod]
public void IsASCII_SimplestValid_ReturnsTrue()
{
    string value = Encoding.ASCII.GetString(new byte[0]);
    var expected = true;

    var actual = value.IsASCII();

    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void IsASCII_MultiByteValid_ReturnsTrue()
{
    string value = "\0\0";
    var expected = true;

    var actual = value.IsASCII();

    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void IsASCII_UnicodeInput_ReturnsFalse()
{
    string value = "Ä€"; // first multibyte utf8 char
    var expected = false;

    var actual = value.IsASCII();

    Assert.AreEqual(expected, actual);
}

Revision: 27580
at June 17, 2010 03:01 by pckujawa


Updated Code
/// <summary>
/// Determines whether the argument string can be represented with the UTF-8 (<see cref="Encoding.UTF8"/>) encoding.
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>
/// <c>true</c> if the specified value is UTF8; otherwise, <c>false</c>.
/// </returns>
public static bool IsUTF8(this string value)
{
    return Encoding.UTF8.GetByteCount(value) == value.Length;
}

[TestMethod]
public void IsUTF8_SimplestValid_ReturnsTrue()
{
    string value = Encoding.UTF8.GetString(new byte[0]);
    var expected = true;

    var actual = value.IsUTF8();

    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void IsUTF8_MultiByteValid_ReturnsTrue()
{
    string value = "\0\0";
    var expected = true;

    var actual = value.IsUTF8();

    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void IsUTF8_UnicodeInput_ReturnsFalse()
{
    string value = "Ä€"; // first multibyte utf8 char
    var expected = false;

    var actual = value.IsUTF8();

    Assert.AreEqual(expected, actual);
}

Revision: 27579
at June 17, 2010 02:52 by pckujawa


Initial Code
/// <summary>
/// Determines whether the argument string can be represented with the UTF-8 (<see cref="Encoding.UTF8"/>) encoding.
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>
/// <c>true</c> if the specified value is UTF8; otherwise, <c>false</c>.
/// </returns>
public static bool IsUTF8(this string value)
{
    return Encoding.UTF8.GetByteCount(value) == value.Length;
}

Initial URL

                                

Initial Description

                                

Initial Title
Check if a string can be represented as ASCII

Initial Tags

                                

Initial Language
C#