代码片收集

JavaScript

  • JavaScript-随机数的生成
    从一组有序的数据中生成一组随机并且不重复的数,类似于简单的抽奖程序的实现。
var gRandomArr = function (arr, length) {
    // 使用sort将原数组的顺序打乱,让有序变成无序
    arr.sort(function () {
        return Math.random() - 0.5;
    });

    // 从原数组中一次性返回10个元素
    return arr.slice(0, length);
};
// 调用
gRandomArr(arr, 10);
  • JavaScript-数值交换
    连续对两个数a和b进行三次异或运算,aˆ=b, bˆ=a, aˆ=b,可以互换它们的值
var a = 10;
var b = 99;
a^=b, b^=a, a^=b;
a // 99
b // 10
  • JavaScript-日期处理格式化函数
    连续对两个数a和b进行三次异或运算,aˆ=b, bˆ=a, aˆ=b,可以互换它们的值
// 日期时间原型增加格式化方法
Date.prototype.Format = function (formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
    var month = this.getMonth() + 1;
    str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
    str = str.replace(/M/g, month);
    str = str.replace(/w|W/g, Week[this.getDay()]);
    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    str = str.replace(/d|D/g, this.getDate());
    str = str.replace(/hh|HH/, this.getHours() > 9 ? his.getHours().toString() : '0' + this.getHours());
    str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());
    str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    str = str.replace(/s|S/g, this.getSeconds());
    return str;
}
//调用
var d = new Date();
var str = d.Format("yyyy-MM-dd  hh:mm:ss");
console.log(str);
  • JavaScript-设置元素长度
$(document).ready(function () {
        $("input[data-val-length-max]").each(function (index, element) {
            var length = parseInt($(this).attr("data-val-length-max"));
            $(this).prop("maxlength", length);
        });
    });

.NET

  • C#-判断两个字符数组的包含关系
bool IsSubSet(string[] b, string[] a)
{
    return b.All(s => a.Contains(s));
}
  • **全角/半角转换 **
// 半角转全角
public static string ToSBC(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }
//全角转半角
public static string ToDBC(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }
The Wipphj ,Hello 光
原文地址:https://www.cnblogs.com/wipphj/p/4550616.html