[英]JavaScript - Get string from position till end of line / return character
I have a string, it gets squished into one line with a (return?) character, but should be like this: 我有一个字符串,使用(return?)字符将其压缩为一行,但是应该像这样:
Service Type: Registration Coordinator
Authorized Amount: $4
Authorized Mileage: $1.25
I want to get 'Registration Coordinator' or whatever is there till the end of the line, so I tried this: 我想获得“注册协调员”或直到该行结束为止的所有内容,因此我尝试了以下操作:
var service_type = t_var.description.substr(t_var.description.indexOf('Service Type: ') +14,t_var.description.indexOf('\n'));
But it returns: 但它返回:
Registration Coordinator
Authorized A
Here is how the original string was created before being posted to the DB, I'm trying to work with it after it has been read back from the DB: 这是在将原始字符串发布到数据库之前创建原始字符串的方式,在从数据库读回原始字符串之后,我尝试使用它:
var fullDescription = "Service Type: " + that.current_wo_data.service_partner.skill + "\n\n";
fullDescription += '\nAuthorized Amount: $' + that.$('#authorized_amount').val();
fullDescription += '\nAuthorized Mileage: $' + that.$('#authorized_mileage').val();
Thanks 谢谢
There is a difference between substr
and substring
(don't ask me why) substr
和substring
之间是有区别的(不要问我为什么)
.substr(start_pos, number_of_chars);
"abcdef".substr(2, 3) returns "cde"
.substring(start_pos, end_pos);
"abcdef".substr(2, 3) returns "c"
You are using start_pos
and end_pos
with the function substr
. 您正在将start_pos
和end_pos
与substr
函数一起使用。 That's asking for trouble ;) 那是自找麻烦;)
var service_type = t_var.description.substr(t_var.description.indexOf('Service Type: ') +14,t_var.description.indexOf('\n'));
Should be 应该
var service_type = t_var.description.substring(t_var.description.indexOf('Service Type: ') +14,t_var.description.indexOf('\n'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.