#js trim
js 自带的
js 自带的
trim 函数是没有参数的,只能移除 white space and line terminator characters (空格和换行) 因此需要移除其他自定符号时请使用工具库的 trim 函数 (如lodash的 _.trim([string=''], [chars=whitespace]) )'abc\n'.trim('a') // 'abc'
_.trim('abc\n', 'a') // 'bc\n'#python
http://blog.jobbole.com/42706/
Python 新手常犯错误(第一部分)
TL;DR
不要用一个可变的值作为默认值
正确做法
http://blog.jobbole.com/42706/
Python 新手常犯错误(第一部分)
TL;DR
不要用一个可变的值作为默认值
>>> def foo(numbers=[]):
... numbers.append(9)
... return numbers
>>> foo()
[9]
>>> foo()
[9, 9]
>>> foo()
[9, 9, 9]
正确做法
def foo(numbers=None):
if numbers is None:
numbers = []
numbers.append(9)
return numbers
如果变量是一个布尔值,变量名最好加上 is、has 或 can 作为前缀https://twitter.com/samantha_ming/status/1043578525339418624
#bug ASCII表中英文字母是不连续的,大写字母跟随着几个符号然后才是小写字符,因此判断字符是否英文字符不能使用 `c >= 'A' && c <= 'z'` 应该使用 `(c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')` 或者使用正则表达式判断 `[A-Za-z]`
#c
-2147483648/-1
整数除以 0 可能会触发 SIGFPE,最小的 INT(-2147483648)除以 -1 也可能会触发
-2147483648/-1
整数除以 0 可能会触发 SIGFPE,最小的 INT(-2147483648)除以 -1 也可能会触发
#include<stdio.h>运行结果
int main() {
int a = -2147483648; // 0x80000000
int b = -1;
int ans = a / b;
printf("%d\n", ans);
return 0;
}
Floating point exceptionForwarded from YSC 的频道
C++11 开始,std::string 可以直接用作缓冲区,不需要用另外一块空间保存,然后再拷贝到 std::string 中。
https://stackoverflow.com/a/39200666
在 C++11/14 中可以使用
https://stackoverflow.com/a/39200666
在 C++11/14 中可以使用
&str.front() 或 &str[0] 来获取地址,C++17 中 str.data() 加入了非 const 的重载,可以直接用 str.data() 来获取地址。str.size() 是缓冲区的长度,可以用 str.resize(length) 分配空间。Stack Overflow
Directly write into char* buffer of std::string
So I have an std::string and have a function which takes char* and writes into it. Since std::string::c_str() and std::string::data() return const char*, I can't use them. So I was allocating a tem...
#js
因此查询普通字符串的时候,请使用`string.indexOf`
'([{'.search('(')
// Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
string.search 字符串的查询方法参数是一个 `RegExp 如果给的是字符串,会被强制转换为 RegExp因此查询普通字符串的时候,请使用`string.indexOf`
#js
判断空数组请不要使用
请使用
判断空数组请不要使用
=== 直接与空数组比较[] === [] // false请使用
if (Array.isArray(array) && array.length === 0) {
// empty array
}#js
js string有两个substr相关的方法
两个方法的去别在于参数不同
string.substr takes parameters as (from, length).
string.substring takes parameters as (from, to).
js string有两个substr相关的方法
string.substr 和 string.substring 两个方法的去别在于参数不同
string.substr takes parameters as (from, length).
string.substring takes parameters as (from, to).
console.log("abc".substr(1,2)); // returns "bc"
console.log("abc".substring(1,2)); // returns "b"#java
java应用打成jar包之后classpath下的resources文件不能用File的方式读取
java应用打成jar包之后classpath下的resources文件不能用File的方式读取
@Value("classpath:menu.json")
private Resource res;
// res.getFile(); ×
res.getInputStream();#java
微信公众平台的某个接口返回了json数据,但是
微信公众平台的某个接口返回了json数据,但是
Content-Type 居然填的的是 text/plain ,导致使用 restTemplate 时无法自动解析,解决方法见上图https://stackoverflow.com/questions/49469954/force-spring-resttemplate-to-process-plain-text-as-json#java
使用
使用
Jackson 解析第三方的接口的json数据时,可以在dto对象上添加注解无视未定义的属性,防止解析时出现 UnrecognizedPropertyException
@JsonIgnoreProperties(ignoreUnknown = true)
class { ... }
#证书
微信公众号开发 测试环境好好的,一部署到正式环境安卓手机的webview就白屏打不开,而IOS表示情绪稳定,经过一番排查发现正式环境使用的https证书链不完整,补全证书链之后访问正常
这个bug难排查在微信webview没有给任何警告报错,我只能看着白屏发呆
证书检查 https://www.geocerts.com/ssl-checker
证书链补全 https://certificatechain.io/
微信公众号开发 测试环境好好的,一部署到正式环境安卓手机的webview就白屏打不开,而IOS表示情绪稳定,经过一番排查发现正式环境使用的https证书链不完整,补全证书链之后访问正常
这个bug难排查在微信webview没有给任何警告报错,我只能看着白屏发呆
证书检查 https://www.geocerts.com/ssl-checker
证书链补全 https://certificatechain.io/
#python
使用crontab运行python时日志乱码
解决方案
在cron中设置
更多信息:https://stackoverflow.com/questions/40718088/encoding-issue-when-running-python-script-from-crontab
使用crontab运行python时日志乱码
解决方案
在cron中设置
LANG 变量* * * * * LANG=en_US.UTF-8 python3 main.py >> log.log
更多信息:https://stackoverflow.com/questions/40718088/encoding-issue-when-running-python-script-from-crontab
Stack Overflow
Encoding issue when running python script from crontab
I am migrating my python scripts from one server to a new docker container. But I am facing a strange issue with encoding, I tried several solutions without success.
If I run the script directly ...
If I run the script directly ...