我試圖在字串中找到一個特定的值,然后檢查該值后面的字符。
我得到了第一部分,可以找到我需要的值,但想在我找到的每個值之后檢查字串中它前面的十個字符中的任何一個是否是數字。
這是我到目前為止的第一部分:
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"
uj5u.com熱心網友回復:
如前所述,您可以使用Regex.Matches例如
var matches = Regex.Matches(Text.text, "\"(.*?)\":(.*?)(?=,|$)");
foreach(var match in matches)
{
var key = match.Groups[1];
var valueString = match.Groups[2];
if(float.TryParse(valueString, out var value))
{
Debug.Log($"Found match for {key} with value = {value}");
}
}
請參閱正則運算式示例和說明
但是,如果這實際上是一個 JSON,那么您應該使用適當的 c# 表示并實際決議 JSON,例如,根據您的 JSON 的復雜性,只需使用內置的 JsonUtility
假設您可以訪問 JSON 生成,并且可以使用不帶空格的名稱,例如
{
"ChestCircumference":12.34,
"BodyHeight":1.23,
"HighHipCircumference":0.23,
"BellyCircumference":0.34567,
"CharacterIndex": 3
}
在你的情況下,你可以例如包裝你的價值觀
// The class name doesn't matter in JSON
[Serializable]
public class Config
{
public float ChestCircumference;
public float BodyHeight;
public float HighHipCircumference;
public float BellyCircumference;
public int CharacterIndex = 0;
}
然后簡單地做
var config = JsonUtility.FromJson<Config>(jsonString);
或者,如果更改 JSON 不是一個選項,請使用Newtonsoft JSON,例如
{
"Chest Circumference":12.34,
"Body Height":1.23,
"High Hip Circumference":0.23,
"Belly Circumference":0.34567,
"Character Index": 3
}
然后
[Serializable]
public class Config
{
[JsonProperty ("Chest Circumference")]
public float ChestCircumference;
[JsonProperty ("Body Height")]
public float BodyHeight;
[JsonProperty ("High Hip Circumference")]
public float HighHipCircumference;
[JsonProperty ("Belly Circumference")]
public float BellyCircumference;
[JsonProperty ("Character Index")]
public int CharacterIndex = 0;
}
然后做
var config = JsonConvert.DeserializeObject<Config>(jsonString);
uj5u.com熱心網友回復:
訪問特定值的最簡單方法是決議字串并將其放入字典中,其中部分的名稱用作鍵:
string input =
"\"Collar Circumference\":39.102776,\"Neck Base Circumference\":42.982479";
var dict = input.Split(",\"")
.Select(p => p.TrimStart('"').Split("\":"))
.ToDictionary(x => x[0], x => Double.Parse(x[1]));
這個測驗...
double collar = dict["Collar Circumference"];
double neckBase = dict["Neck Base Circumference"];
Console.WriteLine(collar);
Console.WriteLine(neckBase);
foreach (var entry in dict) {
Console.WriteLine($"\"{entry.Key}\" = {entry.Value}");
}
... 印刷:
39.102776
42.982479
“領圍” = 39.102776
“頸基圍” = 42.982479
這假設所有值都可以表示為double。如果不是這種情況,請將它們保留為字串:
...
.ToDictionary(x => x[0], x => x[1]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/382303.html
