Uri.IsHexEncoding(字符串,Int32)方法被用来检查在字符串中的字符是否是十六进制编码的或没有。这将检查字符串遵循“ %hexhex ”模式的十六进制编码,其中“ hex ”是从0到9的数字或AF的字母(不区分大小写) 。
Syntax: public static bool IsHexEncoding (string pattern, int index);
Parameters:
pattern: It is the string to check.
index: It is the location in pattern to check for hexadecimal encoding.
Return Value: This method returns a Boolean value that is true if pattern is hexadecimal encoded at the specified location otherwise, false.
下面的程序说明了Uri.IsHexEncoding(String,Int32)方法的用法:
范例1:
// C# program to demonstrate the
// Uri.IsHexEncoding(String,
// Int32) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing pattern
string pattern = "%75";
// Declaring and initializing index
int index = 1;
// Validating the Character in the String
// using IsHexEncoding(String, Int32) method
bool value = Uri.IsHexEncoding(pattern, index);
// Displaying the result
if (value)
Console.WriteLine("{0}({1}) is a valid "+
"Hexadecimal Encoded", pattern);
else
Console.WriteLine("{0} is a valid"+
" Hexadecimal Encoded", pattern);
}
}
输出:
%75 is a valid Hexadecimal Encoded
范例2:
// C# program to demonstrate the
// Uri.IsHexEncoding(String,
// Int32) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// calling get() method
get("%65", 1);
get("%75", 0);
get("%55", 0);
}
// defining get() method
public static void get(string pattern,
int index)
{
// Validating the Character in the String
// using IsHexEncoding(String, Int32) method
bool value = Uri.IsHexEncoding(pattern, index);
// Displaying the result
if (value)
Console.WriteLine("{0} is a valid"+
" Hexadecimal Encoded", pattern);
else
Console.WriteLine("{0} is a not valid"+
" Hexadecimal Encoded", pattern);
}
}
输出:
%65 is a not valid Hexadecimal Encoded
%75 is a valid Hexadecimal Encoded
%55 is a valid Hexadecimal Encoded
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.uri.ishexencoding?view=netstandard-2.1