概述
JS的方法
/*隐藏部分字符
*@str 原字符串
*@startIndex 开始索引
*@length 隐藏长度
*@replaceChar 替换字符
*/
function hiddenChar(str, startIndex, length, replaceChar) {
var str_start = "";
var str_end = "";
var str_middle = "";
var str_length = str.length;
var replace_length = length;
if (startIndex > 0)
str_start = str.substr(0, startIndex);
if (startIndex + replace_length < str_length)
str_end = str.substring(startIndex + replace_length);
if (startIndex + replace_length > str_length)
replace_length = str_length - startIndex;
for (var i = 0; i < replace_length; i++) {
str_middle += replaceChar;
}
str = str_start + str_middle + str_end;
return str;
}
C#的方法
/// <summary>
/// 隐藏部分字符串
/// </summary>
/// <param name="source">原字符串</param>
/// <param name="startIndex">开始隐藏位置</param>
/// <param name="length">隐藏长度</param>
/// <param name="replaceChar">替换符</param>
/// <returns></returns>
public static string HiddenPortion(string source, int startIndex, int length, string replaceChar)
{
string result = null;
string str_start = null;
string str_end = null;
string str_middle = null;
int str_length = source.Length;
int replace_length = length;
if (startIndex >= str_length)
startIndex = str_length;
if (startIndex > 0)
str_start = source.Substring(0, startIndex);
if (startIndex + replace_length < str_length)
str_end = source.Substring(startIndex + replace_length, str_length - startIndex - replace_length);
if (startIndex + replace_length > str_length)
replace_length = str_length - startIndex;
for (var i = 0; i < replace_length; i++)
{
str_middle += replaceChar;
}
result = str_start + str_middle + str_end;
return result;
}
最后
以上就是舒服宝马为你收集整理的.Net分享两个隐藏字符串的方法的全部内容,希望文章能够帮你解决.Net分享两个隐藏字符串的方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复