【发布时间】:2022-01-18 18:25:45
【问题描述】:
我正在尝试在字符串中查找特定值,然后检查该值之后的字符。
我得到了第一部分,可以找到我需要的值,但想在我找到的每个值之后检查字符串中它前面的十个字符中的任何一个是否是数字。
这是我目前得到的第一部分:
public class Text_Value : MonoBehaviour
{
[SerializeField]
private TMP_Text Text;
public List<string> Values = new List<string>();
private int ChestCircumference;
private int BodyHeight;
private int HighHipCircumference;
private int BellyCircumference;
private int CharacterIndex = 0;
// Start is called before the first frame update
void Start()
{
StartCoroutine(GetValues());
}
// Update is called once per frame
void Update()
{
}
IEnumerator GetValues()
{
if (Text.text == "No Data")
{
yield return new WaitForSeconds(0.1f);
Debug.Log("Text Value Finder: Wating For Text Data");
StartCoroutine(GetValues());
}
else if (Text.text != "No Data")
{
foreach (string value in Values)
{
if (Text.text.Contains(value))
{
Debug.Log("Value Found: " + value);
}
else if (!Text.text.Contains(value))
{
Debug.Log("Missing Value: " + value);
}
}
}
}
}
我想我需要在 foreach 循环中使用一个循环,它会遍历接下来的十个字符,但不知道该怎么做。
字符串的一个例子是:
"领围":39.102776,"颈围":42.982479"
【问题讨论】:
-
你所有的
else if应该简单地是else.. 目前你重复地进行两次相同的昂贵检查.. 在第一次检查之后你已经知道条件是否满足...... -
您可能更愿意使用
Regex.Match,这样您就可以找到给定模式的所有匹配项并返回匹配组,例如找到的完全匹配项以及它之前的数值......会更容易如果您能告诉我们您在字符串中查找的确切匹配项,请回答这个问题,包括字符串的预期外观示例 -
你能提供一个示例字符串吗,这会有所帮助
-
@Yot 你是什么意思?
Regex.Match正是为此:在您的字符串中查找特定模式 .. 只是它比搜索精确匹配更灵活,因为您可以定义一个对任何匹配都有效的抽象模式,如果它们都遵循某个特定的相似的模式 -
听起来你更想使用 JSON?