Created
June 5, 2021 12:53
-
-
Save Osinko/a52a36d019bcc6f01eb999de252d567a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using UnityEngine; | |
public class Reserch : MonoBehaviour | |
{ | |
void Start() | |
{ | |
string folder = Application.dataPath; //これだけでunityの実行ファイルがあるフォルダがわかる | |
List<string> strList = new List<string>(); //ロード内容用 | |
LoadText(folder, @"\Gen1.txt", strList); | |
SaveText(folder, @"\Result1.txt", Serch(strList).ToArray()); | |
} | |
//事象数を調べる | |
private List<string> Serch(List<string> dataStr) | |
{ | |
List<string> resultList = new List<string>(); | |
int counter =0; | |
for (int i = 0; i < dataStr.Count; i++) | |
{ | |
if (CountChar(dataStr[i], 'オ') >= 1 && | |
CountChar(dataStr[i], 'チ') >= 2 && | |
CountChar(dataStr[i], 'ン') >= 2) | |
{ | |
counter++; | |
resultList.Add("行番号:" + (i + 1) + " " + dataStr[i]); | |
} | |
} | |
resultList.Add("総数:" + counter); | |
return resultList; | |
} | |
//string内の特定キャラ数を数える | |
//資料:https://www.atmarkit.co.jp/ait/articles/1411/25/news131.html | |
public static int CountChar(string s, char c) | |
{ | |
return s.Length - s.Replace(c.ToString(), "").Length; | |
} | |
//資料:StreamReader クラス (System.IO) | |
//https://docs.microsoft.com/ja-jp/dotnet/api/system.io.streamreader?view=net-5.0 | |
private void LoadText(string fileFolder, string filename, List<string> strList) | |
{ | |
try | |
{ | |
using (StreamReader sr = new StreamReader(fileFolder + filename, System.Text.Encoding.GetEncoding("shift_jis"))) | |
{ | |
string line; | |
while ((line = sr.ReadLine()) != null) | |
{ | |
strList.Add(line); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
print("ファイルが読み込めませんでした:"+ e.Message); | |
} | |
} | |
//資料:StreamWriter クラス (System.IO) | |
//http://msdn.microsoft.com/ja-jp/library/system.io.streamwriter(v=vs.110).aspx | |
//テキストファイルとしてセーブ | |
public void SaveText(string fileFolder, string filename, string[] dataStr) | |
{ | |
using (StreamWriter w = new StreamWriter(fileFolder + filename, false, System.Text.Encoding.GetEncoding("shift_jis"))) | |
{ | |
foreach (var item in dataStr) | |
{ | |
w.WriteLine(item); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment