只是为了完整起见,我想提供一些实现。 一般来说,正则表达式是一种昂贵的方法,特别是如果字符串很大(在传输大文件时会发生这种情况)。以下方法首先尝试最快的检测方式。
public static class HelperExtensions {
// Characters that are used in base64 strings.
private static Char[] Base64Chars = new[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
/// <summary>
/// Extension method to test whether the value is a base64 string
/// </summary>
/// <param name="value">Value to test</param>
/// <returns>Boolean value, true if the string is base64, otherwise false</returns>
public static Boolean IsBase64String(this String value) {
// The quickest test. If the value is null or is equal to 0 it is not base64
// Base64 string's length is always divisible by four, i.e. 8, 16, 20 etc.
// If it is not you can return false. Quite effective
// Further, if it meets the above criterias, then test for spaces.
// If it contains spaces, it is not base64
if (value == null || value.Length == 0 || value.Length % 4 != 0
|| value.Contains(' ') || value.Contains('\t') || value.Contains('\r') || value.Contains('\n'))
return false;
// 98% of all non base64 values are invalidated by this time.
var index = value.Length - 1;
// if there is padding step back
if (value[index] == '=')
index--;
// if there are two padding chars step back a second time
if (value[index] == '=')
index--;
// Now traverse over characters
// You should note that I'm not creating any copy of the existing strings,
// assuming that they may be quite large
for (var i = 0; i <= index; i++)
// If any of the character is not from the allowed list
if (!Base64Chars.Contains(value[i]))
// return false
return false;
// If we got here, then the value is a valid base64 string
return true;
}
}
编辑
至于建议的Sam,你也可以稍微改变源代码。他为最后一步的测试提供了更好的表现方法。常规
private static Boolean IsInvalid(char value) {
var intValue = (Int32)value;
// 1 - 9
if (intValue >= 48 && intValue <= 57)
return false;
// A - Z
if (intValue >= 65 && intValue <= 90)
return false;
// a - z
if (intValue >= 97 && intValue <= 122)
return false;
// + or/
return intValue != 43 && intValue != 47;
}
可以用来代替if (!Base64Chars.Contains(value[i]))
与if (IsInvalid(value[i]))
符合增强的完整源代码Sam看起来像这样(为清楚起见移除评论)
public static class HelperExtensions {
public static Boolean IsBase64String(this String value) {
if (value == null || value.Length == 0 || value.Length % 4 != 0
|| value.Contains(' ') || value.Contains('\t') || value.Contains('\r') || value.Contains('\n'))
return false;
var index = value.Length - 1;
if (value[index] == '=')
index--;
if (value[index] == '=')
index--;
for (var i = 0; i <= index; i++)
if (IsInvalid(value[i]))
return false;
return true;
}
// Make it private as there is the name makes no sense for an outside caller
private static Boolean IsInvalid(char value) {
var intValue = (Int32)value;
if (intValue >= 48 && intValue <= 57)
return false;
if (intValue >= 65 && intValue <= 90)
return false;
if (intValue >= 97 && intValue <= 122)
return false;
return intValue != 43 && intValue != 47;
}
}
这取决于你想要检查的“彻底”。您可以使用正则表达式进行一些预验证,如其他人已经回答的那样,但这不是唯一的指标。 base64编码在某些情况下需要使用“=”符号进行填充。如果填充错误,即使输入与表达式匹配,也会发生错误。 – vcsjones 2011-06-10 16:41:48
您的情况并不完全满足base64字符串。考虑字符串'\ n \ fLE16' - 你的方法会产生误判。对于任何人阅读和寻找一种万无一失的方法;我会建议捕捉FormatException或使用特定的RegEx,请参阅http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data。 – nullable 2017-05-18 20:21:27
如果上述方法返回false,如何将字符串填充到正确的长度? – 2017-07-07 09:55:36