如何解析输入并将不同的值分配给拆分字符串 [英] How to parse an input and assign different values to the split string

查看:84
本文介绍了如何解析输入并将不同的值分配给拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个罗马数字计算器,不确定如何将用户输入解析为3个部分,第一个罗马数字为运算符,第二个数字却忽略了用户可能包括的任何空格,例如XV + L 将在左侧保留,"+"将在op下保留,而右边将在"L"处

Im trying to create a roman numeral calculator and am unsure how I would parse a user input into 3 parts the first roman numeral the operator and the second numeral while ignoring any spaces the user might include for example XV + L the "XV" would be held by the left, the "+" would be held under op and the right would hold "L"

推荐答案

使用indexof函数查找要检查的任何运算符的实例.然后将每个字符放在该索引的左侧(使用子字符串),并将其分配给一个值.将字符放在索引处并将其存储为运算符.取走剩下的所有东西,并将其存储为第二个罗马数字.

Use the indexof function to find instances of any operators you want to check for. Then take every character to the left of that index (using a substring), and assign it to one value. Take the character at the index and store that as the operator. Take everything that is left and store it as your second roman numeral.

这篇关于如何解析输入并将不同的值分配给拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆