zoukankan      html  css  js  c++  java
  • 一个常用的C#读取文本的方法

    个人认为这段代码可读性不是很好,因为我用了这么久看起来还是很不顺眼~! 但是实用还是非常实用的, 记录下来,以后有需要直接取走吧~

    PS:遍历行数读取对应值的时候, 遍历值要+1, 因为遍历默认是从表头开始的,而真正的值是从第二行开始的, +1即从第二行开始遍历

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class LoadText : MonoBehaviour 
    {
    
    	public string dataName;
    
    
    	string[][] Array;
    	string [] allitems;
    	void DescDataInit(){
    		//1到4步的内容是为了把文本内容完全copy下来并且使其可以数据化可以任意读取
    		TextAsset allItemDataBase = Resources.Load("Text/"+dataName, typeof(TextAsset)) as TextAsset;	//1.读取文件
    		
    		allitems = allItemDataBase.text.Split ("
    "[0]);	// 2.读取每一行的内容
    
    		Array = new string [allitems.Length][];		// 3.一个二维数组,行和列分别为: 文本总行数和空
    		
    		for(int i =0;i < allitems.Length; i++){		// 4. 遍历所有文本,以逗号为分隔符存储每一行的内容
    			Array[i] = allitems[i].Split ("," [0]);	
    		}
    	}
    
    
    	string GetDataByIdAndName(string strName,int nId){  
    		if (Array.Length <= 0)  
    			return "";  
    		
    		int nRow = Array.Length;  
    		int nCol = Array[0].Length;   
    		
    		for (int i = 0; i < nRow; i++) { //行 
    			if (i == nId) {  
    				for (int j = 0; j < nCol; j++) { //列
    					if (Array[0][j] == strName) {  
    						return Array[i][j];  
    					}  
    				}
    			}  
    		}  
    		return "";  
    	} 
    
    
    }
    
  • 相关阅读:
    springBoot异常处理
    webSocket
    Java正则
    String.format
    JSTL-taglib
    http meta
    chrome 常用插件下载安装
    mysql 命令行个性化设置
    Entity Framework Code First
    SignalR Connection has not been fully initialized
  • 原文地址:https://www.cnblogs.com/hellozzz/p/4654710.html
Copyright © 2011-2022 走看看