日常代码积累

1. 字符的全半角转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// <summary>
/// 转全角(SBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>全角字符串</returns>
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);
}
/// <summary>
/// 转半角(DBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>半角字符串</returns>
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);
}

2. javascript 大小写转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

//转换成大写人民币
var digitUppercase = function (n) {
var fraction = ['角', '分'];
var digit = [
'零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖'
];
var unit = [
['元', '万', '亿'],
['', '拾', '佰', '仟']
];
var head = n < 0 ? '欠' : '';
n = Math.abs(n);
var s = '';
for (var i = 0; i < fraction.length; i++) {
s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
}
s = s || '整';
n = Math.floor(n);
for (var i = 0; i < unit[0].length && n > 0; i++) {
var p = '';
for (var j = 0; j < unit[1].length && n > 0; j++) {
p = digit[n % 10] + unit[1][j] + p;
n = Math.floor(n / 10);
}
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
}
return head + s.replace(/(零.)*零元/, '元')
.replace(/(零.)+/g, '零')
.replace(/^整$/, '零元整');
}

3. 获取两个List或数组的差集与交集

3.1. 差集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
List<int> list1 = new List<int>();
list1.Add(1);
list1.Add(2);
list1.Add(3);
List<int> list2 = new List<int>();
list2.Add(3);
list2.Add(4);
list2.Add(5);
//得到的结果是4,5 即减去了相同的元素。
List<int> list3 = list2.Except(list1).ToList();
foreach (int i in list3)
{
MessageBox.Show(i.ToString());
}

3.2. 合并两个数组,并去掉重复元素,然后排序(C#)

1
2
3
4
5
6
7
List<int> numbers1 = new List<int>() { 5, 4, 1, 3, 9, 8, 6, 7, 12, 10 };
List<int> numbers2 = new List<int>() { 15, 14, 11, 13, 19, 18, 16, 17, 12, 10 };
var newQuerty = numbers1.Concat(
from n in numbers2
where !numbers1.Contains(n)
select n
).OrderBy(n=>n);

3.3. 合并两个数组,并去除合并后的重复数据, 并排序

1
2
3
4
int[] A={1,2,2,3,4,5,6,6,6};
int[] B={2,2,2,3,7,8,9,5};
List<int> list = new List<int>(A);
list.AddRange(B);

4. List<父类>引用List<子类>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Father
{

}
public class Son:Father
{
}

public class Test
{
public void Test()
{
List<Father> fathers = new List<Father>();
List<Son> sons = new List<Sons>();
sons = sons.Cast<Father>().ToList();
}
}

List<T> 的一些概念

  • List<T>初始值大小是4,自动扩容是以当前数组元素的两倍或InsertRange目标List的元素个数来扩容(如个大选如个)。如果比较确定的大小可以考虑提前设置,因为每次自动扩容需要重新分配数组和copy元素,性能损耗不小。
  • List<T>通过version来跟踪集合是否发生改变,如果在foreach遍历时发生改变则发生异常。
  • List<T>并非线程安全,任何使用的时候都要考虑当前环境是否可能有多线程存在,是否需要用锁来保证集合线程安全。

EasyUI Dialog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function DlogOwner(url) {
$("#dlg").dialog({
title: '人员信息',
modal: true,
closed: true,
href: url,
queryParams: { type: 'add', income_id: $("#income_id3").val() },
buttons: [{
text: '取消',
handler: function () {
$('#dlg').dialog('close');
}
}],
onClose: function () {
var income_id = $("#income_id3").val();
FillNewOwner(income_id);
},
//content: "<iframe scrolling='auto' frameborder='0' src='" + url + "' style='width:100%; height:100%; display:block;'></iframe>"
});
}
function getQueryParam(name) {
var obj = $('#dd').dialog('options');
var queryParams = obj["queryParams"];

return queryParams[name];
}

5. 隐藏下拉框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
select {  
/*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/
border: solid 1px #000;

/*很关键:将默认的select选择框样式清除*/
appearance:none;
-moz-appearance:none;
-webkit-appearance:none;

/*在选择框的最右侧中间显示小箭头图片*/
background: url("http://ourjs.github.io/static/2015/arrow.png") no-repeat scroll right center transparent;


/*为下拉小箭头留出一点位置,避免被文字覆盖*/
padding-right: 14px;
}


/*清除ie的默认选择框样式清除,隐藏下拉箭头*/
select::-ms-expand { display: none; }

6. JavaScript日期转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Date.prototype.Format = function (fmt) { //author: meizz 
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

return fmt;

}
var maydate = new Date();
console.log(" mydate :" + maydate.Format("yyyy-MM-dd hh:mm:ss"));