从文本文件中的某一行读取一个数字

本文关键字:读取 一行 一个 数字 文本 文件 | 更新日期: 2023-09-27 18:07:58

我想做的是分别读取文本文件的每一行,首先找到一个特定的字符串,如果找到该字符串,则读取该行中的整数。

字符串是这样的:

{
"SmartCursorToggle": true,
"MapEnabled": true,
"InvasionBarMode": 2,
"AutoSave": true,
"AutoPause": false,
"Language": 1,
"PlacementPreview": true,
"GoreVisualsAllowed": true,
"VolumeSound": 1.0,
"VolumeAmbient": 0.75,
"VolumeMusic": 0.75,
"KeyUp": "W",
"KeyDown": "S",
"KeyLeft": "A",
"KeyRight": "D",
"KeyJump": "Space",
"KeyThrowItem": "T",
"KeyInventory": "Escape",
"KeyQuickHeal": "H",
"KeyQuickMana": "J",
"KeyQuickBuff": "B",
"KeyUseHook": "E",
"KeyAutoSelect": "LeftShift",
"KeySmartCursor": "LeftControl",
"KeyMount": "R",
"KeyMapStyle": "Tab",
"KeyFullscreenMap": "M",
"KeyMapZoomIn": "Add",
"KeyMapZoomOut": "Subtract",
"KeyMapAlphaUp": "PageUp",
"KeyMapAlphaDown": "PageDown",
"Fullscreen": false,
"WindowMaximized": false,
"DisplayWidth": 800,
"DisplayHeight": 704,
"GraphicsQuality": 0,
"BackgroundEnabled": true,
"FrameSkip": true,
"LightingMode": 0,
"LightingThreads": 0,
"MouseColorR": 252,
"MouseColorG": 233,
"MouseColorB": 221,
"Parallax": 90.0,
"ShowItemText": true,
"LastLaunchedVersion": 155,
"ClientUUID": "7d49a838-d7db-4e74-8124-92552a429491642949429491609524294916095159563f7c",
"UseSmartCursorForCommonBlocks": false,
"UseSmartAxeAfterSmartPickaxe": false,
"UseSmartWallReplacement": true,
"DisableLeftShiftTrashCan": false,
"HighlightNewItems": true,
"HidePasswords": false,
"ThickMouseEdges": true,
"ThickMouseEdgesPackedColor": 4289397106,
"ReverseUpDownForArmorSetBonuses": false,
"CloudSavingDefault": false
}

"{"括号在文本文件中。

假设我想找到鼠标颜色的R值,我将把每一行放入字符串数组中,看看索引(I)处的字符串是否包含"MouseColorR",我如何获得该行中的整数??

从文本文件中的某一行读取一个数字

正如Stefan提到的那样,最好的方法是使用JSON。净:

var json = JsonConvert.DeserializeObject<Data>(str);
var value = json.MouseColorR;

str是json输入字符串。

Data类:

public class Data
{
    public bool SmartCursorToggle { get; set; }
    public bool MapEnabled { get; set; }
    public int InvasionBarMode { get; set; }
    public bool AutoSave { get; set; }
    public bool AutoPause { get; set; }
    public int Language { get; set; }
    public bool PlacementPreview { get; set; }
    public bool GoreVisualsAllowed { get; set; }
    public float VolumeSound { get; set; }
    public float VolumeAmbient { get; set; }
    public float VolumeMusic { get; set; }
    public string KeyUp { get; set; }
    public string KeyDown { get; set; }
    public string KeyLeft { get; set; }
    public string KeyRight { get; set; }
    public string KeyJump { get; set; }
    public string KeyThrowItem { get; set; }
    public string KeyInventory { get; set; }
    public string KeyQuickHeal { get; set; }
    public string KeyQuickMana { get; set; }
    public string KeyQuickBuff { get; set; }
    public string KeyUseHook { get; set; }
    public string KeyAutoSelect { get; set; }
    public string KeySmartCursor { get; set; }
    public string KeyMount { get; set; }
    public string KeyMapStyle { get; set; }
    public string KeyFullscreenMap { get; set; }
    public string KeyMapZoomIn { get; set; }
    public string KeyMapZoomOut { get; set; }
    public string KeyMapAlphaUp { get; set; }
    public string KeyMapAlphaDown { get; set; }
    public bool Fullscreen { get; set; }
    public bool WindowMaximized { get; set; }
    public int DisplayWidth { get; set; }
    public int DisplayHeight { get; set; }
    public int GraphicsQuality { get; set; }
    public bool BackgroundEnabled { get; set; }
    public bool FrameSkip { get; set; }
    public int LightingMode { get; set; }
    public int LightingThreads { get; set; }
    public int MouseColorR { get; set; }
    public int MouseColorG { get; set; }
    public int MouseColorB { get; set; }
    public float Parallax { get; set; }
    public bool ShowItemText { get; set; }
    public int LastLaunchedVersion { get; set; }
    public string ClientUUID { get; set; }
    public bool UseSmartCursorForCommonBlocks { get; set; }
    public bool UseSmartAxeAfterSmartPickaxe { get; set; }
    public bool UseSmartWallReplacement { get; set; }
    public bool DisableLeftShiftTrashCan { get; set; }
    public bool HighlightNewItems { get; set; }
    public bool HidePasswords { get; set; }
    public bool ThickMouseEdges { get; set; }
    public long ThickMouseEdgesPackedColor { get; set; }
    public bool ReverseUpDownForArmorSetBonuses { get; set; }
    public bool CloudSavingDefault { get; set; }
}

如果你不想使用JSON。如果你只需要这个值,你可以使用Regex:

var regex = new Regex("'MouseColorR': (''d{3})");
Match match = regex.Match(str);
if (match.Success)
{
    string v = match.Groups[1].Value;
}

您可以使用Dynamic访问任何JSON对象中的已知属性。假设您正在使用JSON。如其他回答和评论中提到的。NET,并且包含Json对象的文本文件名存储在变量fileName中,代码将如下:

dynamic json = JsonConvert.DeserializeObject(File.ReadAllText(fileName));
var value = json.MouseColorR