将 Javascript 转换为 Objective C(数字的后 4 位)
Posted
技术标签:
【中文标题】将 Javascript 转换为 Objective C(数字的后 4 位)【英文标题】:Convert Javascript to Objective C (Last 4 digits of number) 【发布时间】:2020-05-05 10:05:44 【问题描述】:请耐心等待我的编程。我对 Objective C 还是很陌生。我想将以下 javascript 代码转换为 Objective C。我只想显示路由和帐号的最后 4 位数字。
<script>
var routing = document.getElementById('routing_number');
var changed1 = document.getElementById('routing_number');
changed1.value = new Array(routing.value.length-3).join('x') + routing.value.substr(routing.value.length-4, 4);
var account = document.getElementById('account_number');
var changed2 = document.getElementById('account_number');
changed2.value = new Array(account.value.length-3).join('x') + account.value.substr(account.value.length-4, 4);
</script>
我在 Objective C 中尝试了以下部分代码,但没有成功。换句话说,我希望将 javascript 中的内容准确地翻译成 Objective C。非常感谢任何帮助。
_routingText.text = [Utilities removeNullFromString:[response valueForKey:@"routing_number"]];
NSString *routing_number = _routingText.text;
routing_number = [routing_number substringToIndex:(routing_number.length - 4)];
【问题讨论】:
routing_number = [routing_number substringFromIndex: [routing_number length] - 4];
。您可以尝试使用 substringFromIndex 而不是 substringToIndex。
@SumeshSivan 嘿,感谢您的回复。然而,这并没有奏效。
【参考方案1】:
我为任何想要下面答案的人找到了答案。
NSString *routing_number = [Utilities removeNullFromString:[response valueForKey:@"routing_number"]];
NSString *last4Chr = [routing_number substringFromIndex: [routing_number length] - 4];
NSMutableString *mask = [[NSMutableString alloc]init];
for (int i=0; i<[routing_number length]-4; i++)
[mask appendString:@"*"];
[mask appendString:last4Chr];
_routingText.text = mask;
NSString *account_number = [Utilities removeNullFromString:[response valueForKey:@"account_number"]];
NSString *last4Chr2 = [account_number substringFromIndex: [account_number length] - 4];
NSMutableString *mask2 = [[NSMutableString alloc]init];
for (int i=0; i<[account_number length]-4; i++)
[mask2 appendString:@"*"];
[mask2 appendString:last4Chr2];
_acc_NumText.text = mask2;
【讨论】:
以上是关于将 Javascript 转换为 Objective C(数字的后 4 位)的主要内容,如果未能解决你的问题,请参考以下文章
javascript 将arraybuffer转换为string,将string转换为arraybuffer
javascript 怎么将字符串转换为ArrayBuffer