ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2023ǯ05·î29Æü(·î) 11:49:51ÍúÎò
¤³¤Î¼ê½ç¤Ç¤Ï¡¢List ¤Î¾ðÊó¤ò¥»¡¼¥Ö¡¦¥í¡¼¥É¤¹¤ë¤¿¤á¤Îµ¡Ç½¤ò¼ÂÁõ¤·¤Þ¤¹¡£
¡¡°Ê²¼¤ÎÆâÍƤǼÂÁõ¤ò¿Ê¤á¤Æ¤¤¤¤Þ¤¹¡£
ȯŸ£±£¸¡¡¡¼¥»¡¼¥Ö¡¦¥í¡¼¥Éµ¡Ç½¤Î¼ÂÁõ¢¡¼
£²£¶¡¥¥¯¥ê¥¢¤·¤¿¥¹¥Æ¡¼¥¸¤Î¾ðÊó¤È·ÀÌó¤·¤¿¥¥ã¥é¤Î¾ðÊó¤ò¥»¡¼¥Ö¡¦¥í¡¼¥É¤¹¤ëµ¡Ç½¤ò¼ÂÁõ¤¹¤ë
¡¡¿·¤·¤¤³Ø½¬ÆâÍƤϡ¢°Ê²¼¤ÎÄ̤ê¤Ç¤¹¡£
¡¡¡¦String.IsNullOrEmpty ¥á¥½¥Ã¥É ¡¡¡¦String.Split ¥á¥½¥Ã¥É¤È StringSplitOptions Îóµó·¿ ¡¡¡¦Linq¤Îµ¡Ç½¤Î¼ÂÁõÎ㢡¡¡ÁAggregate ¥á¥½¥Ã¥É¡¢ToList ¥á¥½¥Ã¥É¡Á ¡¡¡¦List¤Î»È¤¤Êý£¡¡¡¼ConvertAll ¥á¥½¥Ã¥É¡¼ ¡¡¡¦int32.TryParse ¥á¥½¥Ã¥É¡¢int32.Parse ¥á¥½¥Ã¥É
¡¡Á°²ó¤Î¼ê½ç¤Ç¤Ï int ·¿¤Î¥¯¥ê¥¢¥Ý¥¤¥ó¥È¤Î¾ðÊó¤ò PlayerPrefs ¥¯¥é¥¹¤Î SetInt ¥á¥½¥Ã¥É¤È GetInt ¥á¥½¥Ã¥É¤ò ³èÍѤ·¡¢¥»¡¼¥Ö¡¦¥í¡¼¥Éµ¡Ç½¤ò¼ÂÁõ¤·¤Þ¤·¤¿¡£
¤³¤Î¼ê½ç¤Ç¤Ï¡¢¥¯¥ê¥¢¥Ý¥¤¥ó¥È¤Î¾¤Ë¤â¥»¡¼¥Ö¡¦¥í¡¼¥É¤ò¹Ô¤¤¤¿¤¤¾ðÊó¤ò¹Í¤¨¤Æ¡¢¤½¤ÎÊÑ¿ô¤ò¥»¡¼¥Ö¡¦¥í¡¼¥É¤¹¤ëµ¡Ç½¤ò¼ÂÁõ¤¹¤ëÀ߷פò¹Í¤¨¤Æ¤¤¤¤Þ¤¹¡£
¡¡¸½ºß¥æ¡¼¥¶¡¼¤Î¾ðÊó¤È¤·¤Æ¥»¡¼¥Ö¤·¤Æ¤ª¤¤¿¤¤¾ðÊó¤È¤·¤Æ¤Ï¡¢¥¯¥ê¥¢¤·¤¿¥¹¥Æ¡¼¥¸¤Î¾ðÊó¤È¡¢·ÀÌ󤷤Ƥ¤¤ë¥¥ã¥é¤Î¾ðÊ󤬤¢¤²¤é¤ì¤Þ¤¹¡£
¤É¤Á¤é¤â int ·¿¤Î¾ðÊó¤Ç¤¹¤¬¡¢List ¤ò³èÍѤ·¤Æ´ÉÍý¤ò¤ª¤³¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
¡¡PlayerPrefs ¥¯¥é¥¹¤Ë¤Ï int ·¿¤ò¥»¡¼¥Ö¡¦¥í¡¼¥É¤¹¤ë¥á¥½¥Ã¥É¤Ï¤¢¤ê¤Þ¤¹¤¬¡¢List ·¿¤ò¤½¤Î¤Þ¤Þ¥»¡¼¥Ö¡¦¥í¡¼¥É¤¹¤ëµ¡Ç½¤Ï¤¢¤ê¤Þ¤»¤ó¡£
¤Ä¤Þ¤ê¡¢List<int> ·¿¤½¤Î¤Þ¤Þ¤Î¾õÂ֤Ǥϥ»¡¼¥Ö¡¦¥í¡¼¥É¤¬¤Ç¤¤Ê¤¤¤¿¤á¡¢¾¯¤·¼ê¤ò²Ã¤¨¤Æ¤¢¤²¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
¡¡¤³¤Î¤è¤¦¤Ê¤È¤¤Ë¤Ï¤¤¤¯¤Ä¤«¤ÎÊýË¡¤¬¹Í¤¨¤é¤ì¤Þ¤¹¡£(¾¤Ë¤â¤¢¤ê¤Þ¤¹)
¡ã·¿¤¬°Û¤Ê¤ëºÝ¤Î¥»¡¼¥Ö¤ÎÊýË¡¡ä
¡¡£±¡¥List ¤ÎÍ×ÁÇ(Ãæ¿È)¤ò£±¤Ä¤º¤Ä¼è¤ê½Ð¤·¤Æ¡¢int ·¿¤È¤·¤Æ£±¤Ä¤º¤Ä¡¢¥»¡¼¥Ö¡¦¥í¡¼¥É¤ò¹Ô¤¦ ¡¡£²¡¥Êݸ¤·¤¿¤¤¥Ç¡¼¥¿¤Î·¿¤Îºî¤ê¤ò¥»¡¼¥Ö¡¦¥í¡¼¥É¤¬¹Ô¤¨¤ë·¿¤ËÊѹ¹¤·¤Æ¤«¤é¡¢¥»¡¼¥Ö¡¦¥í¡¼¥É¤ò¹Ô¤¦
¡¡º£²ó¤Ï¡Ú£²¡Û¤ÎÊýË¡¤òºÎÍѤ·¤Þ¤¹¡£
¡¡»²¹Í¤Þ¤Ç¤Ë¤Ç¤¹¤¬¡¢¡Ú£±¡Û¤ÎÊýË¡¤ò²òÀ⤷¤Æ¤ª¤¤Þ¤¹¡£
for ʸ¤ä foreach ʸ¤Ê¤É¤ò³èÍѤ·¤Æ¡¢List ¤ÎºÇÂçÃͤòÌÜɸÃͤ˷«¤êÊÖ¤·¤Î½èÍý¤òºî¤ê¡¢½èÍý¤ÎÃæ¤Ç¡¢List ¤Î³ÆÍ×ÁǤ˽çÈ֤˥¢¥¯¥»¥¹¤·¤Æ¡¢¤½¤ì¤ò int ·¿¤Ç¥»¡¼¥Ö¤ò¹Ô¤¤¤Þ¤¹¡£
¥í¡¼¥É¤¹¤ë¾ì¹ç¤âƱÍͤǡ¢int ·¿¤Î¾ðÊó¤ò¥í¡¼¥É¤·¤Æ¡¢¤½¤ì¤ò List ¤Ë½çÈÖ¤ËÄɲ乤뤳¤È¤ÇÉü¸µ¤·¤Þ¤¹¡£¤³¤ì¤Ï¡¢List ¤Ç´ÉÍý¤·¤Æ¤¤¤ë·¿¤¬¥»¡¼¥Ö¡¦¥í¡¼¥É¤Ç¤¤ë·¿¤Ç¤¢¤ë¾ì¹ç¤Ë¤Î¤ß¼ÂÁõ½ÐÍè¤Þ¤¹¡£
¡¡¤¿¤À¤·¡¢¤³¤Î¼ÂÁõ¤Î¾ì¹ç¡¢¥»¡¼¥Ö¡¦¥í¡¼¥ÉÍѤΠKey ¤¬³ÆÃͤ´¤È¤ËɬÍפˤʤê¤Þ¤¹¡£Î㤨¤Ð¡¢List ¤ÎÍ×ÁÇ¿ô¤¬ 5 ¸Ä¤¢¤ë¾ì¹ç¡¢5 ¤Ä¤Î¥»¡¼¥Ö¡¦¥í¡¼¥ÉÍѤΠKey ¤¬É¬Íפˤʤê¤Þ¤¹¡£
¤³¤Î¤è¤¦¤Ê¾ì¹ç¤Ï¡¢£±¤Ä¤Î Key ÍѤÎʸ»úÎó¤òºîÀ®¤·¡¢Ê¸»ú¤ÎºÇ¸å¤ËÄ̤·ÈÖ¹æ¤òºÎÈÖ¤¹¤ë¤³¤È¤Ç³ÆÃͤΥ»¡¼¥Ö¡¦¥í¡¼¥ÉÍѤΠKey ¤ò¼«Æ°Åª¤ËºîÀ®¤Ç¤¤Þ¤¹¡£
¡ãfor ʸ¤ò³èÍѤ·¤Æ List ¤Î¾ðÊó¤ò¥»¡¼¥Ö¡¦¥í¡¼¥É¤¹¤ë¾ì¹ç¤Î¼ÂÁõÎã¡ä
private const string ENGAGE_CHARA_KEY = "engageCharaNos_";¡¡¡¡//¡¡¥»¡¼¥Ö¡¦¥í¡¼¥ÉÍѤΠKey(¤³¤Î¤Þ¤Þ¤À¤È£±¤Ä¤·¤«¥»¡¼¥Ö¤Ç¤¤Ê¤¤¤Î¤Ç¡¢ºÇ¸å¤ËÈÖ¹æ¤òÄɲ乤ë) ¡¡¡¡// ¥»¡¼¥Ö public void SaveEngageCharaListToInt() { // ·ÀÌó¤·¤¿¥¥ã¥é¤Î List ¤«¤éÍ×ÁǤò 1¤Ä¤º¤Ä¼è¤ê½Ð¤¹ for (int i = 0; i < engageCharaNosList.Count; i++) { ¡¡¡¡¡¡¡¡¡¡¡¡// £±¤Ä¤º¤Ä¥»¡¼¥Ö¤¹¤ë¤¿¤á¤Ë Key ¤ËºÎÈÖ¤·¡¢¥»¡¼¥Ö¤¹¤ëÃͤòÀßÄê PlayerPrefs.SetInt(ENGAGE_CHARA_KEY + engageCharaNosList[i].ToString(), engageCharaNosList[i]); // engageCharaNos_0 ¤Î¤è¤¦¤Ê·Á¤Ç¥»¡¼¥Ö¤µ¤ì¤Þ¤¹¤Î¤Ç¡¢¥í¡¼¥É¤¹¤ë¾ì¹ç¤â¡¢engageCharaNos_0 ¤Î¤è¤¦¤Ë¡¢ºÇ¸å¤ËÈÖ¹æ¤òÉÕ¤±¤ëɬÍפ¬¤¢¤ê¤Þ¤¹ } // SetInt ¥á¥½¥Ã¥É¤Î¾ðÊó¤ò¤¹¤Ù¤Æ¥»¡¼¥Ö PlayerPrefs.Save(); } ¡¡¡¡// ¥í¡¼¥É public void LoadEngageCharaListToInt() { // ÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¥¥ã¥é¤Î¥Ç¡¼¥¿¿ô¤òÌÜɸÃͤ˷«¤êÊÖ¤¹ for (int i = 0; i < DataBaseManager.instane.charaDataSO.charaDataList.Count; i++) { // ¥»¡¼¥Ö¤µ¤ì¤Æ¤¤¤ë Key ¤¬¤¢¤ë¤«³Îǧ if(PlayerPrefs.HasKey(ENGAGE_CHARA_KEY + i)) { ¡¡¡¡¡¡¡¡¡¡¡¡ // ¥í¡¼¥É¤·¤Æ List ¤ËÄɲà engageCharaNosList.Add(PlayerPrefs.GetInt(ENGAGE_CHARA_KEY + i)); } } }
¡¡¡Ú£²¡¥Êݸ¤·¤¿¤¤¥Ç¡¼¥¿¤Î·¿¤Îºî¤ê¤ò¥»¡¼¥Ö¡¦¥í¡¼¥É¤¬¹Ô¤¨¤ë·¿¤ËÊѹ¹¤·¤Æ¤«¤é¡¢¥»¡¼¥Ö¡¦¥í¡¼¥É¤ò¹Ô¤¦¡ÛÊýË¡¤Ë¤Ä¤¤¤Æ¤ÎÀ߷פιͤ¨Êý¤Ç¤¹¡£
¡¡·¿¤Îºî¤ê¤ò¥»¡¼¥Ö¡¦¥í¡¼¥É¤¬¹Ô¤¨¤ë·¿¤ËÊѹ¹¤¹¤ë¡¢¤È¤¤¤¦Éôʬ¤Ç¤¹¤¬¡¢PlayerPrefs ¥¯¥é¥¹¤Ç¤Ï£³¤Ä¤Î·¿¤Ç¤Î¥»¡¼¥Ö¡¦¥í¡¼¥É¤¬²Äǽ¤Ç¤¹¡£
List ¤Î¾ðÊó¤Ï int ·¿¤Ç¤¹¤¬¡¢¤³¤Î¾ðÊó¤ò¡¢ÃÍ(Í×ÁÇ)¤´¤È¤Ë¥«¥ó¥Þ¤Ç¶èÀÚ¤é¤ì¤¿£±¹Ô¤Îʸ»úÎó¤È¤·¤Æ¿·¤·¤¯ÊÑ¿ô¤òºîÀ®¤¹¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
¡ãList<int> ¤ò¥«¥ó¥Þ¶èÀÚ¤ê¤Î string ·¿¤Ë¤¹¤ë¡ä
List¡ãint¡ä engageCharaNosList = new List<int>(); engageCharaNosList[0] = 0; engageCharaNosList[1] = 1; engageCharaNosList[2] = 2; engageCharaNosList[3] = 3;
¡¡¡¡¢
// List<int> ¤ò ¥«¥ó¥Þ¶èÀÚ¤ê¤Î 1¹Ô¤Î string ·¿¤Ë¤¹¤ë string saveStr = "0, 1, 2, 3,";
¡¡¤³¤Î¤è¤¦¤Ë¡¢¾ðÊó¤ò½ñ¤´¹¤¨¤Æ¤¢¤²¤ë¤³¤È¤Ç¡¢PlayerPrefs ¥¯¥é¥¹¤Î¥»¡¼¥Ö¡¦¥í¡¼¥É¤¬¤Ç¤¤ë string ·¿¤Ë¤Ê¤ê¤Þ¤¹¤Î¤Ç¡¢
¥»¡¼¥Ö¤ò¹Ô¤¦ºÝ¤Ë string ·¿¤Ë¤·¤ÆÊݸ¤·¡¢¥í¡¼¥É¤ò¹Ô¤¦ºÝ¤Ë¤Ï¤³¤Î string ·¿¤Ç¥í¡¼¥É¤·¤Æ¡¢¤½¤Î¾ðÊó¤ò¸µ¤Î·¿¤Î¾ðÊó¤ËÌ᤹¤³¤È¤Ç¡¢
PlayerPrefs ¥¯¥é¥¹¤òÍøÍѤ·¤Æ¥»¡¼¥Ö¤È¥í¡¼¥É¤¬¹Ô¤¨¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
¡¡½èÍý¤Îή¤ì¤ò¤Þ¤È¤á¤Þ¤¹¡£
¡ã¥»¡¼¥Ö¤¹¤ë¤È¤¡ä
¡¡¡¦¥»¡¼¥Ö¤·¤¿¤¤¾ðÊó(¥¯¥é¥¹¡¦·¿)¤¬ PlayerPrefs ¥¯¥é¥¹¤ËÂбþ¤·¤Æ¤¤¤ë·¿¤Ç¤Ï¤Ê¤¤¤¿¤á¡¢¤½¤Î¤Þ¤Þ¤Ç¤Ï List<int> ·¿¤ÎÊÑ¿ô¤ÎÃͤò¥»¡¼¥Ö¡¦¥í¡¼¥É¤Ç¤¤Ê¤¤¡£ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¢ ¡¡¡¦List<int> ·¿¤Î¾ðÊó¤ò£±¤Ä¤º¤Ä¼è¤ê½Ð¤·¤Æ¡¢ÃͤÈÃͤȤò¥«¥ó¥Þ¤Ç¶èÀڤä¿£±¹Ô¤Îʸ»úÎó¤È¤·¤ÆºîÀ®¤·¡¢¥»¡¼¥Ö¤·¤¿¤¤¾ðÊó¤ò string ·¿¤Î¾ðÊó¤ËÊÑ´¹¤¹¤ë¡£ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¢ ¡¡¡¦¤³¤Î string ·¿¤Î¾ðÊó¤ò PlayerPrefs ¥¯¥é¥¹¤Î SetString ¥á¥½¥Ã¥É¤È Save ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ¥»¡¼¥Ö¤¹¤ë¡£
¡ã¥í¡¼¥É¤¹¤ë¤È¤¡ä
¡¡¡¦string ·¿¤Ç¥»¡¼¥Ö¤·¤Æ¤¢¤ë¾ðÊó¤ò PlayerPrefs ¥¯¥é¥¹¤Î GetString ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ¥í¡¼¥É¤ò¤¹¤ë¡£ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¢ ¡¡¡¦¥í¡¼¥É¤·¤¿¾ðÊó(¥¯¥é¥¹¡¦·¿)¤¬ string ·¿¤Î¤¿¤á¡¢¤½¤Î¤Þ¤Þ¤Ç¤Ï¥»¡¼¥Ö¤¹¤ëÁ°¤Î¾ðÊó(º£²ó¤Ï List<int> ·¿)¤È¤·¤Æ³èÍѤǤ¤Ê¤¤¡£ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¢ ¡¡¡¦¥í¡¼¥É¤·¤¿¾ðÊó¤ò¥«¥ó¥Þ¤Î°ÌÃ֤ǶèÀڤäơ¢List<int> ·¿¤ÎÃͤȤ·¤Æ£±¤Ä½çÈ֤˼è¤ê½Ð¤¹¡£¤½¤ÎºÝ¤Ë¡¢string ·¿¤ò int ·¿¤ËÊÑ´¹¤·¤Æ List ¤ËÄɲ乤뤳¤È¤Ç¾ðÊó¤òÉü¸µ¤¹¤ë¡£ ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¢ ¡¡¡¦¥»¡¼¥Ö¤¹¤ëºÝ¤Î¾ðÊó(¥¯¥é¥¹)¤ËÉü¸µ¤µ¤ì¤ë¤Î¤Ç¡¢¤³¤Î¾ðÊó¤ò¤Þ¤¿¥²¡¼¥à¤ÇÍøÍѤ¹¤ë
¡¡IsNullOrEmpty ¥á¥½¥Ã¥É¤Ï String ¥¯¥é¥¹¤¬»ý¤Äµ¡Ç½¤Î£±¤Ä¤Ç¤¹¡£ÍøÍѤ¹¤ë¤¿¤á¤Ë¤Ï¡¢using System ¤ÎÀë¸À¤¬É¬Íפˤʤê¤Þ¤¹¡£
¡¡°ú¿ôÆâ¤Ë»ØÄꤵ¤ì¤¿Ê¸»úÎó¤¬ null ¡¢¤¢¤ë¤¤¤Ï¶õ¤Îʸ»úÎó ("") ¤Ç¤¢¤ë¤«¤É¤¦¤«¤òȽÄꤷ¤Æ¿¿µ¶ÃͤòÌᤷ¤Þ¤¹¡£
null¡¢¤¢¤ë¤¤¤Ï¶õ¤Îʸ»úÎó ("") ¤Ç¤¢¤ë¾ì¹ç¤Ë¤Ï true ¡¢Ê¸»úÎó¤¬ null ¤Ç¤Ê¤¤¾ì¹ç¤Ë¤Ï false ¤òÌᤷ¤Þ¤¹¡£
¡¡Ãͤ¬¤¢¤ë¤«¡¢¤Ç¤Ï¤Ê¤¯¡¢Ãͤ¬¤Ê¤¤¤«¤Ë¼ç¤òÃÖ¤¤¤¿È½Äê¤Ç¤¹¤Î¤Ç¡¢¿¿µ¶ÃͤλȤ¤Êý¤ò´Ö°ã¤¨¤Ê¤¤¤è¤¦¤Ë¤·¤Þ¤·¤ç¤¦¡£
¡¡¤Ä¤Þ¤ê¡¢Ê¸»úÎó¤¬Æþ¤Ã¤Æ¤¤¤ë¤³¤È¤ò´üÂÔ¤¹¤ë¤Î¤Ç¤¢¤ì¤Ð¡¢false ¤Ç¤¢¤ë¤³¤È¤òȽÄêÃͤ˻Ȥ¦É¬Íפ¬¤¢¤ê¤Þ¤¹¡£
¡¡// ¥í¡¼¥É¤·¤¿Ê¸»úÎ󤬤¢¤ë¾ì¹ç if (!string.IsNullOrEmpty(engageCharaListString)) {
¡¡º£²ó¤Î¥±¡¼¥¹¤Î¾ì¹ç¡¢°ú¿ô¤È¤·¤Æ»ØÄꤵ¤ì¤Æ¤¤¤ë¤Î¤Ï¡¢engageCharaListString ÊÑ¿ô¤ÎÉôʬ¤Ç¤¢¤ê¡¢¤³¤ÎÃͤ¬È½Äê¤ÎÂоݤǤ¹¡£
engageCharaListString ÊÑ¿ô¤ÎÃͤ¬ null ¤Ê¤Î¤«¡¢¤¢¤ë¤¤¤ÏÃͤ¬¤¢¤ë¤Î¤«¤òȽÄꤷ¤Æ¡¢¿¿µ¶ÃͤòÌ᤹½èÍý¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
»²¹Í¥µ¥¤¥È
MicroSoft C# ¥É¥¥å¥á¥ó¥È
String.IsNullOrEmpty(String) ¥á¥½¥Ã¥É
@IT ÍÍ
ʸ»úÎó¤¬null¡ÊNothing¡Ë¤«¶õʸ»úÎ󤫤ò´Êñ¤ËȽÄꤹ¤ë¤Ë¤Ï¡©
¡¡Split ¥á¥½¥Ã¥É¤Ï String ¥¯¥é¥¹¤¬»ý¤Äµ¡Ç½¤Î£±¤Ä¤Ç¤¹¡£ÍøÍѤ¹¤ë¤¿¤á¤Ë¤Ï¡¢using System ¤ÎÀë¸À¤¬É¬Íפˤʤê¤Þ¤¹¡£
Split ¥á¥½¥Ã¥É¤Ë¤Ï°ú¿ô¤Î¥ª¡¼¥Ð¡¼¥í¡¼¥É¤¬Â¿¤¯¤¢¤ê¤Þ¤¹¤¬¡¢º£²ó¤Ï¡¢Split(String[], StringSplitOptions) ¤Î½ñ¼°¤Î°ú¿ô¤òÍøÍѤ·¤Æ¤¤¤Þ¤¹¡£
¡¡
»²¹Í¥µ¥¤¥È
MicroSoft
String.Split ¥á¥½¥Ã¥É
¡¡º£²ó¤Î¾ì¹ç¤Î Split ¥á¥½¥Ã¥É¤Ï¡¢Âè1°ú¿ô¤Ë»ØÄꤷ¤¿Ê¸»úÎó¤Ç¡¢ÂоݤȤʤëʸ»úÎóÆâ¤ò¶èÀڤäơ¢£±¤Ä¤Îʸ»úÎó¤òʬ³ä¤¹¤ë¾ì¹ç¤Ë»ÈÍѤ·¤Þ¤¹¡£
// ¥«¥ó¥Þ¤Î°ÌÃ֤ǶèÀڤäơ¢Ê¸»úÎó¤ÎÇÛÎó¤òºîÀ®¡£¤½¤ÎºÝ¡¢ºÇ¸å¤Ë¤Ç¤¤ë¶õÇò¤Îʸ»úÎó¤òºï½ü string[] strArray = engageCharaListString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
¡¡º£²ó¤Î¾ì¹ç¤Ç¤¢¤ì¤Ð¡¢engageCharaListString ¤¬Ê¬³ä¤ÎÂоݤȤʤëʸ»úÎó¤Ç¤¹¡£
¤³¤Îʸ»úÎó¤òÀèƬ¤«¤éÄ´ºº¤·¤Æ¤¤¤¡¢Âè1°ú¿ô¤Ç»ØÄꤷ¤Æ¤¤¤ë , (¥«¥ó¥Þ)¤ÎÉôʬ¤Ç¶èÀڤꡢʬ³ä¤·¤Þ¤¹¡£Ê¬³ä¸å¡¢¤½¤ì¤òÇÛÎó¤È¤·¤Æ strArray ÊÑ¿ô¤ËÂåÆþ¤·¤Æ¤¤¤Þ¤¹¡£
¡¡Î㤨¤Ð¡¢engageCharaListString ÊÑ¿ô¤ÎÃͤ¬ {0, 1, 2} ¤Ç¤¢¤ë¤Ê¤é¤Ð¡¢¤³¤ì¤ò¥«¥ó¥Þ¤Î°ÌÃ֤ǶèÀڤꡢ£³¤Ä¤Îʸ»úÎó¤Ëʬ³ä¤·¤Æ¤¤¤Þ¤¹¡£
¤½¤ì¤òÇÛÎó¤È¤·¤ÆÂåÆþ¤·¤Æ¤¤¤Þ¤¹¡£
¡¡StringSplitOptions Îóµó·¿¤Ï¡¢Split ¥á¥½¥Ã¥É¤Ë»ØÄê¤Ç¤¤ë¥ª¥×¥·¥ç¥óµ¡Ç½¤Ç¤¹¡£º£²ó¤Î°ú¿ô¤Î¾ì¹ç¤Ï¡¢Âè2°ú¿ô¤È¤·¤ÆÀßÄê¤Ç¤¤Þ¤¹¡£
¡¡StringSplitOptions Îóµó·¿¤Ë¤Ï£³¼ïÎà¤ÎÎóµó»Ò¤¬¤¢¤ê¡¢¤³¤Îµ¡Ç½¤ò³èÍѤ¹¤ë¤È¡¢ÊÖ¤µ¤ì¤¿ÇÛÎ󤫤é¤Î¶õ¤ÎÉôʬʸ»úÎó¤ò¾Êά¤¹¤ë¤«¤É¤¦¤«¡¢
¤Þ¤¿¤ÏÉôʬʸ»úÎ󤫤é¶õÇò¤ò¥È¥ê¥ß¥ó¥°¤¹¤ë¤«¤É¤¦¤«¤Ê¤É¤Î¥ª¥×¥·¥ç¥ó¤ò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡º£²ó¤Ï RemoveEmptyEntries ¤ÎÎóµó»Ò¤ò»ØÄꤷ¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢Ê¸»úÎó¤òʬ³ä¤·¤Æ¤¤¤Ã¤Æ¶õÇò¤Î¾ðÊ󤬺îÀ®¤µ¤ì¤¿¾ì¹ç¡¢¤½¤ì¤ò¾Êά(¶õÇò¤òºï½ü)¤·¤Æ¤¤¤Þ¤¹¡£
¤½¤Î¤¿¤á¡¢ÇÛÎó¤ÎÍ×ÁǤˤ϶õÇò¤Î¾ðÊó¤¬ÂåÆþ¤µ¤ì¤Ê¤¤¤è¤¦¤ËÀ©¸æ¤·¤Æ¤¤¤Þ¤¹¡£
»²¹Í¥µ¥¤¥È
MicroSoft
StringSplitOptions Îóµó·¿
¡¡GameData ¥¯¥é¥¹¤Î Awake ¥á¥½¥Ã¥ÉÆâ¤Î¥Ç¥Ð¥Ã¥°¤Î½èÍý¤ò³èÍѤ·¤Æ¡¢¥Ç¥Ð¥Ã¥°¤ò¹Ô¤¤¤Þ¤¹¡£
¥Ç¥Ð¥Ã¥°¤ÎÊýË¡¤Ï¡¢Á°²ó¤Î¥¯¥ê¥¢¥Ý¥¤¥ó¥È¤Î¾ì¹ç¤ÈƱ¤¸¤Ç¤¹¡£
Awake ¥á¥½¥Ã¥ÉÆâ¤Ë¥»¡¼¥ÖÍѤΥ᥽¥Ã¥É¤Î¸Æ¤Ó½Ð¤·Ì¿Îá¤È¥í¡¼¥ÉÍѤΥ᥽¥Ã¥É¤Î¸Æ¤Ó½Ð¤·Ì¿Î᤬¥³¥á¥ó¥È¥¢¥¦¥È¤µ¤ì¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢
¤½¤ì¤ò³èÍѤ·¤Æ¥Ç¥Ð¥Ã¥°¤ò¹Ô¤Ã¤Æ¤¯¤À¤µ¤¤¡£
¡¡List ¤Î¥»¡¼¥Ö¤È¥í¡¼¥É¤Î½èÍý¤Î¥Ç¥Ð¥Ã¥°¤¬Ìµ»ö¤Ë½ªÎ»¤·¤¿¤é¡¢¼¡¤Ï¡¢¤É¤Î¥¿¥¤¥ß¥ó¥°¤Ç¥»¡¼¥Ö¤È¥í¡¼¥É¤ò¹Ô¤¦¤Î¤«¤ò¹Í¤¨¤Æ¡¢
¤½¤ÎÉôʬ¤Ë¥»¡¼¥Ö¤È¥í¡¼¥É¤Î¥á¥½¥Ã¥É¤ò¼Â¹Ô¤¹¤ë½èÍý¤ò¼ÂÁõ¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£¥í¡¼¥É¤Ë¤Ä¤¤¤Æ¤Ï¡¢Á°²ó¤Î¥¯¥ê¥¢¥Ý¥¤¥ó¥È¤ÈƱ¤¸¤Ç
¥²¡¼¥à³«»Ï»þ¤Ë£±²ó¤À¤±¼Â¹Ô¤¹¤ì¤Ð¤¤¤¤¤Î¤Ç¡¢¥»¡¼¥Ö¤¹¤ëÉôʬ¤ò¹Í¤¨¤Æ¤ß¤Æ¤¯¤À¤µ¤¤¡£
¡¡GameData ¥¯¥é¥¹¤Î SaveEngageCharaList ¥á¥½¥Ã¥É¤È LoadEngageCharaList ¥á¥½¥Ã¥É¤Î½èÍý¤òÌá¤êÃÍ¤È Linq ¤Î¥á¥½¥Ã¥É¤ò³èÍѤ·¤Æ¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤ò¹Ô¤¤¤Þ¤¹¡£
¡¡ºÇ½é¤Ë SaveEngageCharaList ¥á¥½¥Ã¥É¤«¤é¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤ò¹Ô¤¤¤Þ¤¹¡£
Ìá¤êÃͤò»ý¤Ä ConvertListToString ¥á¥½¥Ã¥É¤ò¿·¤·¤¯ºîÀ®¤·¡¢¤½¤Î¥á¥½¥Ã¥ÉÆâ¤Ç¡¢
¤¤¤Þ¤Þ¤Ç SaveEngageCharaList ¥á¥½¥Ã¥É¤Ë¤ª¤¤¤Æ½èÍý¤·¤Æ¤¤¤¿ÆâÍƤò Linq ¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ¼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¡£
¡ãSaveEngageCharaList ¥á¥½¥Ã¥É¤Î¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¡ä
using System.Linq; // <= Äɲä·¤Þ¤¹ ¡¡¡¡¡ÊÃæά¡Ë /// <summary> /// engageCharaNosList ¤ÎÃͤò¥»¡¼¥Ö /// </summary> public void SaveEngageCharaList() { ////*¡¡¤³¤³¤«¤é¥³¥á¥ó¥È¥¢¥¦¥È¡¡*//// // ¿·¤·¤¯ºîÀ®¤¹¤ëʸ»úÎó //string engageCharaListString = ""; // ·ÀÌó¤·¤¿¥¥ã¥é¤Î List ¤ò¥«¥ó¥Þ¶èÀÚ¤ê¤Î£±¹Ô¤Îʸ»úÎó¤Ë¤¹¤ë //for (int i = 0; i < engageCharaNosList.Count; i++) { // engageCharaListString += engageCharaNosList[i].ToString() + ","; //} // ʸ»úÎó¤ò¥»¥Ã¥È¤·¤Æ¥»¡¼¥Ö //PlayerPrefs.SetString(ENGAGE_CHARA_KEY, engageCharaListString); ////*¡¡¤³¤³¤Þ¤Ç¡¡*//// // ¾åµ¤Î½èÍý¤ò£±¹Ô¤Çµ½Ò¤·¤Þ¤¹¡£SetString ¥á¥½¥Ã¥É¤ÎÂè2°ú¿ôÆâ¤Ç¤Ï ConvertListToString ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤·¡¢string ·¿¤ÎÌá¤êÃͤò¼õ¤±¼è¤ê¡¢¤½¤ì¤òÂè2°ú¿ô¤È¤·¤ÆÀßÄꤷ¤Æ¤¤¤Þ¤¹ PlayerPrefs.SetString(ENGAGE_CHARA_KEY, ConvertListToString(engageCharaNosList)); PlayerPrefs.Save(); } /// <summary> /// int ·¿¤Î List ¤ÎÃͤò¥«¥ó¥Þ¶èÀÚ¤ê¤Î1¹Ô¤Î string ·¿¤ËÊÑ´¹ /// </summary> /// <param name="listData"></param> /// <returns></returns> public string ConvertListToString(List<int> listData) { ¡¡¡¡¡¡¡¡// ¤³¤Î½èÍý¤Ï Linq ¤Îµ¡Ç½¤òÍøÍѤ·¤Æµ½Ò¤µ¤ì¤Æ¤¤¤Þ¤¹¡£ ¡¡¡¡¡¡¡¡// ¤³¤Î½èÍý¤Ï¡¢GameData ¥¯¥é¥¹¤Î SaveEngageCharaData ¥á¥½¥Ã¥ÉÆâ¤Î½èÍý¤Î¡¢List<int> ·¿¤Î¾ðÊó¤ò string ·¿(£±¹Ô¤Î¥«¥ó¥Þ¶èÀÚ¤ê¤Îʸ»úÎó)¤Ë¤¹¤ë½èÍý¤ÈƱ¤¸¤â¤Î¤Ç¤¹ return listData.Select(x => x.ToString()).Aggregate((a, b) => a + "," + b); }
¡¡Â³¤¤¤Æ¡¢LoadEngageCharaList ¥á¥½¥Ã¥É¤Î¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤ò¹Ô¤¤¤Þ¤¹¡£
¸½ºß¤ÏÈó¾ï¤ËŤ¤½èÍý¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¤¬¡¢¤³¤Á¤é¤â¡¢Ìá¤êÃͤò»ý¤Ä ConvertStringToList ¥á¥½¥Ã¥É¤ò¿·¤·¤¯ºîÀ®¤·¤Æ¡¢Linq ¤ÇÍÑ°Õ¤µ¤ì¤Æ¤¤¤ë¥á¥½¥Ã¥É¤ò³èÍѤ¹¤ë¤³¤È¤Ë¤è¤ê¡¢
¥¹¥Þ¡¼¥È¤Ê½ñ¼°¤Ë½ñ¤´¹¤¨¤ë¤³¤È¤¬²Äǽ¤Ç¤¹¡£
¡ãLoadEngageCharaList ¥á¥½¥Ã¥É¤Î¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¡ä
/// <summary> /// engageCharaNosList ¤ÎÃͤò¥í¡¼¥É /// </summary> public void LoadEngageCharaList() { ////*¡¡¤³¤³¤«¤é¥³¥á¥ó¥È¥¢¥¦¥È¡¡*//// // ʸ»úÎó¤È¤·¤Æ¥í¡¼¥É //string engageCharaListString = PlayerPrefs.GetString(ENGAGE_CHARA_KEY, ""); // ¥í¡¼¥É¤·¤¿Ê¸»úÎ󤬤¢¤ë¾ì¹ç //if (!string.IsNullOrEmpty(engageCharaListString)) { // ¥«¥ó¥Þ¤Î°ÌÃ֤ǶèÀڤäơ¢Ê¸»úÎó¤ÎÇÛÎó¤òºîÀ®¡£¤½¤ÎºÝ¡¢ºÇ¸å¤Ë¤Ç¤¤ë¶õÇò¤Îʸ»úÎó¤òºï½ü // string[] strArray = engageCharaListString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); // Debug.Log(strArray.Length); // ÇÛÎó¤Î¿ô¤À¤±·ÀÌó¤·¤¿¥¥ã¥é¤Î¾ðÊ󤬤¢¤ë¤Î¤Ç // for (int i = 0; i < strArray.Length; i++) { // Debug.Log(strArray[i]); // ÇÛÎó¤Îʸ»úÎó¤ÎÃͤò int ·¿¤ËÊÑ´¹¤·¤Æ List ¤ËÄɲ䷤ơ¢·ÀÌó¥¥ã¥é¤Î List ¤òÉü¸µ // engageCharaNosList.Add(int.Parse(strArray[i])); // } //} ////*¡¡¤³¤³¤Þ¤Ç¡¡*//// // ¾åµ¤Î½èÍý¤ò£±¹Ô¤Çµ½Ò¤·¤Þ¤¹¡£ ConvertStringToList ¥á¥½¥Ã¥É¤Î°ú¿ô¤È¤·¤Æ GetString ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤·¡¢List<int> ·¿¤ÎÌá¤êÃͤò¼õ¤±¼è¤ê¡¢¤½¤ì¤ò engageCharaNosList ¤ËÀßÄꤷ¤Æ¤¤¤Þ¤¹ engageCharaNosList = ConvertStringToList(PlayerPrefs.GetString(ENGAGE_CHARA_KEY, "")); } /// <summary> /// ¥«¥ó¥Þ¶èÀÚ¤ê¤Ë¤Ê¤Ã¤Æ¤¤¤ë1¹Ô¤Î string ·¿¤ÎÃͤò int ·¿¤Î List ¤ËÊÑ´¹ /// </summary> /// <param name="str"></param> /// <returns></returns> public List<int> ConvertStringToList(string str) { ¡¡¡¡if (string.IsNullOrEmpty(str)) { ¡¡¡¡Debug.Log($"str ÊÑ¿ô¤ÎÃÍ : {str}"); ¡¡¡¡return null; ¡¡¡¡} ¡¡¡¡¡¡¡¡// ¤³¤Î½èÍý¤Ï Linq ¤Îµ¡Ç½¤òÍøÍѤ·¤Æµ½Ò¤µ¤ì¤Æ¤¤¤Þ¤¹¡£ // GameData ¥¯¥é¥¹¤Î LoadEngageCharaData ¥á¥½¥Ã¥ÉÆâ¤Î½èÍý¤Î¡¢string ·¿(£±¹Ô¤Î¥«¥ó¥Þ¶èÀÚ¤ê¤Îʸ»úÎó)¤ò List<int> ·¿¤Ë¤¹¤ë½èÍý¤ÈƱ¤¸¤Ç¤¹ return str.Split(',').ToList().ConvertAll(x => int.Parse(x)); }
¡¡¿·¤·¤¯¼ÂÁõ¤·¤Æ¤¤¤ë Linq ¤Î¥á¥½¥Ã¥É¤Ë¤Ä¤¤¤Æ½çÈÖ¤ËÀâÌÀ¤ò¹Ô¤¤¤Þ¤¹¡£
Linq ¤Îµ¡Ç½¤òÍøÍѤ¹¤ë¤¿¤á¤Ë¤Ï using System.Linq ¤ÎÀë¸À¤¬É¬ÍפǤ¹¡£
¡¡¥¢¥°¥ê¥²¡¼¥È¤ÈÆɤߤޤ¹¡£±Ññ¸ì¤È¤·¤Æ¤Ï¡¢½¸·×¤È¤¤¤¦°ÕÌ£¤ò»ý¤Á¤Þ¤¹¡£
listData.Select(x => x.ToString()).Aggregate((a, b) => a + "," + b);
¡¡Select ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ int ·¿¤Î¾ðÊó¤¬ string ·¿¤Ë·¿ÊÑ´¹¤µ¤ì¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢¤½¤Î string ·¿¤Î¾ðÊó¤ËÂФ·¤Æ¡¢Aggregate ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£
¤³¤Î¥á¥½¥Ã¥É¤Ï¡¢ºîÍѤ¹¤ë½èÍý¤Î·¿¤Î¾ðÊó¤òÌá¤êÃͤȤ·¤Æ»ý¤Á¤Þ¤¹¡£¤½¤Î¤¿¤áº£²ó¤Ç¤¢¤ì¤Ð¡¢string ·¿¤ÎÌá¤êÃͤò»ý¤Ä½èÍý¤Ë¤Ê¤ê¤Þ¤¹¡£
¡¡Aggregate ¥á¥½¥Ã¥É¤Î°ú¿ô¤Ï¥é¥à¥À¼°¤Ë¤è¤ëµ½Ò¤ò¹Ô¤¦·è¤Þ¤ê¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
Âè1°ú¿ô¤Ï a ÊÑ¿ô¤òÀë¸À¤·¤Æ¤¤¤ëÉôʬ¤Ç¤¢¤ê¡¢¤³¤³¤Ë¤Ï½¸·×·ë²Ì¤¬ÂåÆþ¤µ¤ì¤Þ¤¹¡£
Âè2°ú¿ô¤Ï b ÊÑ¿ô¤òÀë¸À¤·¤Æ¤¤¤ëÉôʬ¤Ç¤¢¤ê¡¢¤³¤³¤Ë¤Ï Select ¥á¥½¥Ã¥É¤Î½èÍý·ë²Ì¤Ç¤¢¤ë string ·¿¤Î List ¤Î¾ðÊó¤¬Í×ÁǤȤ·¤ÆÂåÆþ¤µ¤ì¤Æ¡¢£±¤Ä¤º¤Ä½çÈ֤˽èÍý¤¬¹Ô¤ï¤ì¤Þ¤¹¡£
¡¡
¡¡½èÍý¤ÎÆâÍƤϡ¢=> ¤Î±¦Â¦¤ÎÉôʬ¤ËÀßÄꤷ¤Þ¤¹¡£º£²ó¤Ï a + "," + b ¤ÈÀßÄꤷ¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢List ¤ÎºÇ½é¤Îʸ»úÎó¤Ë , (¥«¥ó¥Þ)¤ò²Ã¤¨¤Æ¤«¤é¡¢List ¤Î¼¡¤Îʸ»úÎó¤ò¤·¤Þ¤¹¡£
¤³¤ì¤ò¡¢List ¤ÎÍ×ÁǤ¬Ìµ¤¯¤Ê¤ë¤Þ¤Ç·«¤êÊÖ¤·¡¢ºÇ½ªÅª¤Ë string ·¿¤Î¾ðÊó¤¬ÃͤȤ·¤ÆºîÀ®¤µ¤ì¤Þ¤¹¡£
¡¡Î㤨¤Ð¡¢listData ÊÑ¿ô¤ÎÃͤ¬ {0, 2, 4} ¤Ç¤¢¤Ã¤¿¤È¤·¤¿¾ì¹ç¡¢ºÇ½é¤Ë¡¢Select ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Æ¡¢¤³¤Î£³¤Ä¤ÎÃÍ¤Ï int ·¿¤«¤é string ·¿¤Ë·¿ÊÑ´¹¤µ¤ì¤Þ¤¹¡£
¼¡¤Ë Aggregate ¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤Æ¡¢string ·¿¤Î List ¤ÎÍ×ÁǤ¬£±¤Ä¤º½àÈǤ˼è¤ê½Ð¤µ¤ì¤Þ¤¹¡£
¡¡a + "," + b ¤ÎÉôʬ¤Ë¼Â¿ôÃͤòÅö¤Æ¤Ï¤á¤Æ¤ß¤ë¤È
a + "," + b 0,2 (a = 0 b = 2) 0,2,4 (a = 0,2 b = 4)
¡¡¤È¤¤¤¦½èÍý¤Ë¤Ê¤ê¤Þ¤¹¡£¤è¤Ã¤Æ¡¢Aggregate ¥á¥½¥Ã¥É¤Î½èÍý¤Î·ë²Ì¡¢List ¤ÎÍ×ÁǤ¬£±¤Ä¤Îʸ»úÎó¤ÎÃͤˤʤê¤Þ¤¹¡£
¡ã»²¹Í¥µ¥¤¥È¡ä
MicroSoft
Enumerable.Aggregate ¥á¥½¥Ã¥É
https://docs.microsoft.com/ja-jp/dotnet/api/system...
.NET Column ÍÍ
¡ÚC#¤ÈLINQ¡ÛAggregate¥á¥½¥Ã¥É¤ò»È¤Ã¤Æ¥Ç¡¼¥¿¤Î½¸·×¤ò¤¹¤ëÊýË¡
https://www.fenet.jp/dotnet/column/language/2048/
¥²¡¼¥Þ¡¼¤È¤¤É¤¥¨¥ó¥¸¥Ë¥¢ ÍÍ
¡ÚC#¡Û¡ÚLINQ¡ÛAggregate¥á¥½¥Ã¥É¤Î»È¤¤Êý¤ò²òÀ⤷¤Þ¤¹
https://www.tairax.com/entry/Csharp/LINQ-Aggregate
¡¡ÇÛÎó¤ä¡¢Linq ¤Î Select ¥á¥½¥Ã¥É¡¢Where ¥á¥½¥Ã¥É¤Î·ë²Ì¤ò List ¤Î¥¤¥ó¥¹¥¿¥ó¥¹(¼ÂÂÎ)¤È¤·¤ÆºîÀ®¤·¤Þ¤¹¡£
Select ¥á¥½¥Ã¥É¤Ê¤É¤ÎÌá¤êÃͤǤ¢¤ë IEnumerable·¿¤ÎÃͤÏÃê½Ð¤·¤¿¤À¤±¤Ç List ¤Î¾õÂ֤ˤϤʤäƤ¤¤Þ¤»¤ó¤Î¤Ç¡¢Ãê½Ð¤·¤¿¾ðÊó¤ò List ¤È¤·¤ÆÍøÍѤ·¤¿¤¤¾ì¹ç¤Ë¤Ï
¤³¤Î ToList ¥á¥½¥Ã¥É¤Î½èÍý¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ë¤è¤ê List ¤È¤·¤ÆÃͤ¬ºîÀ®¤µ¤ì¤Þ¤¹¡£
¡ã»²¹Í¥µ¥¤¥È¡ä
MicroSoft
Enumerable.ToList<TSource>(IEnumerable<TSource>) ¥á¥½¥Ã¥É
https://docs.microsoft.com/ja-jp/dotnet/api/system...
Ķ½é¿´¼Ô¸þ¤±¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç ÍÍ
LINQ¤Î¼çÍץ᥽¥Ã¥É°ìÍ÷
https://programming.pc-note.net/csharp/linq_method...
¥ä¥¹¥Î¡¼¥È¤Ïº£Æü¤â¹Í¤¨¤ë ÍÍ
[C# LINQ] ÇÛÎó¤äSelect¡¢Where¤·¤¿·ë²Ì¤ò¥ê¥¹¥È¤ËÊÑ´¹¤¹¤ë
https://yaspage.com/prog/csharp/cs-linq-tolist/
¡¡List ¤Î»ý¤Ä¥á¥½¥Ã¥É¤Î£±¤Ä¤Ç¤¹¡£Array(ÇÛÎó)¤Ë¤âƱ̾¡¦Æ±µ¡Ç½¤Î¥á¥½¥Ã¥É¤¬¤¢¤ê¤Þ¤¹¡£
¡¡ConvertAll ¥á¥½¥Ã¥É¤òÍøÍѤ¹¤ë¤È¡¢°ú¿ô¤Ë»ØÄꤷ¤¿Í×ÁǤò¤¹¤Ù¤ÆÊ̤η¿¤ËÊÑ´¹¡Ê¥¥ã¥¹¥È¡Ë½èÍý¤ò¹Ô¤¦¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
str.Split(',').ToList().ConvertAll(x => int.Parse(x));
¡¡x ÊÑ¿ô¤ÎÉôʬ¤Ë¤Ï List ¤ÎÍ×ÁǤ¬½çÈÖ¤ËÂåÆþ¤µ¤ì¤Þ¤¹¡£
º£²ó¤Î¼ÂÁõÎã¤Ç¤Ï¡¢string ·¿¤Î List ¤Î¤¹¤Ù¤Æ¤ÎÍ×ÁǤò ConvertAll ¥á¥½¥Ã¥É¤òÍøÍѤ·¡¢ int.Paruse ¥á¥½¥Ã¥É¤Ë¤è¤Ã¤Æ int ·¿¤Ë·¿ÊÑ´¹¤¹¤ë½èÍý¤ò¼Â¹Ô¤·¤Æ¤¤¤Þ¤¹¡£
¤³¤ì¤Ë¤è¤ê¡¢Ìá¤êÃͤη¿¤Ç¤¢¤ë List<int> ·¿¤ÎÃͤ¬ºîÀ®¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
public List<int> ConvertStringToList(string str) { return str.Split(',').ToList().ConvertAll(x => int.Parse(x)); // <=¡¡½èÍý¤¬ºÇ¸å¤Þ¤Ç´°Î»¤¹¤ë¤³¤È¤ÇÌá¤êÃͤη¿¤Ç¤¢¤ë List<int> ·¿¤ÎÃͤˤʤë }
¡¡¤Ê¤ª¤³¤Î½èÍý¤Ï¡¢¤¹¤Ç¤Ë³Ø½¬¤·¤Æ¤¤¤ë Linq ¤Î Select ¥á¥½¥Ã¥É¤ò³èÍѤ¹¤ë¤³¤È¤Ç¡¢Æ±¤¸µ¡Ç½¤È¤·¤Æ¼ÂÁõ¤¹¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
return str.Split(',').ToList().ConvertAll(x => int.Parse(x));
¡¡¡¡¡¡¡¡¢
// ¤³¤Á¤é¤Î½èÍý¤âƱ¤¸µ¡Ç½¤È¤·¤ÆÆ°ºî¤·¤Þ¤¹ ¡¡return str.Split(',').Select(x => int.Parse(x)).ToList();
¡¡¿§¡¹¤Ê½èÍý¤Îµ¡Ç½¤Èµ½ÒÊýË¡¤ò³Ð¤¨¤Æ¤¤¤¯¤³¤È¤Ç¡¢¤è¤êÉý¤Î¹¤¤¡¢°Û¤Ê¤ë½èÍý¤Îµ½ÒÊýË¡¤Ë±þÍѤ·¤Æ¤¤¤¯¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
¡ã»²¹Í¥µ¥¤¥È¡ä
MicroSoft
List<T>.ConvertAll<TOutput>(Converter<T,TOutput>) ¥á¥½¥Ã¥É
https://docs.microsoft.com/ja-jp/dotnet/api/system...
ConvertAll¥á¥½¥Ã¥É¤ò»È¤Ã¤Æʸ»úÎ󷿤ÎList¤«¤é¿ôÃÍ·¿¤ÎList¤ËÊÑ´¹¤¹¤ë
https://smdn.jp/programming/dotnet-samplecodes/col...
¡¡Ê¸»úÎó·¿(string)¤ò¿ôÃͤËÊÑ´¹¤¹¤ëÊýË¡¤Î£±¤Ä¤È¤·¤Æ¡¢¿ôÃÍ·¿ (int¡¢long¡¢double ¤Ê¤É) ¤Ë¤Ï TryParse ¤Þ¤¿¤Ï Parse ¥á¥½¥Ã¥É¤¬ÍÑ°Õ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
ÍøÍѤ¹¤ë¤¿¤á¤Ë¤Ï¡¢using System ¤ÎÀë¸À¤¬É¬Íפˤʤê¤Þ¤¹¡£
¡¡º£²ó¤Î¥±¡¼¥¹¤Ç¤Ï¡¢ConvertAll ¥á¥½¥Ã¥É¤Î°ú¿ôÆâ¤Ë¤ª¤¤¤Æ int.Parse ¥á¥½¥Ã¥É¤òÍøÍѤ·¡¢Ê¬³ä¤µ¤ì¤Æ List ²½¤µ¤ì¤¿³Æʸ»úÎó¤ò£±¤Ä¤º¤Ä½çÈÖ¤ËÀ°¿ôÃͤËÃÖ¤´¹¤¨¤Æ¤¤¤Þ¤¹¡£
public List<int> ConvertStringToList(string str) { return str.Split(',').ToList().ConvertAll(x => int.Parse(x)); }
¡¡Î㤨¤Ð¡¢x ÊÑ¿ô¤¬ "1" ¤È¤¤¤¦Ê¸»úÎó¤Ç¤¢¤ë¾ì¹ç¤Ë¤Ï¡¢ int ·¿¤Î 1 ¤È¤¤¤¦¾ðÊó¤ËÊÑ´¹¤µ¤ì¤Þ¤¹¡£
¡¡¾åµ¤Î½èÍý¤ò¤·¤Ã¤«¤ê¤ÈÍý²ò¤·¤¿¾å¤Ç¤Ë¤Ê¤ê¤Þ¤¹¤¬¡¢¤³¤Î½èÍý¤Ï¡¢Linq ¤Î Select ¥á¥½¥Ã¥É¤ò³èÍѤ¹¤ë¤³¤È¤Ç¡¢
int.TryParse ¥á¥½¥Ã¥É¤òÍøÍѤ·¤¿¡¢¤è¤ê°ÂÁ´¤Ê½èÍý¤Ë¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°½ÐÍè¤Þ¤¹¡£
¡¡Æñ¤·¤¤ÆâÍƤǤ¢¤ë¤¿¤á¡¢¤¤¤Þ¤Ï¥¹¥¥Ã¥×¤·¤Æ¤ª¤¤¤Æ¤âÂç¾æÉפǤ¹¡£
¡ãTryParse ¤ËÃÖ¤´¹¤¨¡ä
public List<int> ConvertStringToList(string str) { return str.Split(',') ¡¡¡¡.Select(x => int.TryParse(x, out int i) ? i : -1) ¡¡¡¡.ToList(); }
¡¡¤³¤³¤Ç¤ÎÊѹ¹¤Ï¡¢
.ConvertAll(x => int.Parse(x))
¤ò
.Select(x => int.TryParse(x, out int i) ? i : -1).ToList()
¡¡¤ËÃÖ¤´¹¤¨¤Æ¤¤¤Þ¤¹¡£
¡¡int.TryParse(x, out int i) ? i : -1¤È¤¤¤¦É½¸½¤Ï¡¢°ú¿ôÆâ¤Ë»°¹à±é»»»Ò¤òÍøÍѤ·¡¢int.TryParse¤ò»È¤Ã¤Æʸ»úÎó¤òÀ°¿ô¤ËÊÑ´¹¤·¤è¤¦¤È»î¤ß¤Æ¤¤¤Þ¤¹¡£
À®¸ù¤·¤¿¾ì¹ç¤Ï¤½¤ÎÀ°¿ô¤ò¡¢¼ºÇÔ¤·¤¿¾ì¹ç¤Ï -1 ¤ò¥ê¥¹¥È¤ËÄɲä·¤Þ¤¹¡£¤³¤ÎÎã¤Ç¤Ï¡¢²òÀϤ˼ºÇÔ¤·¤¿¾ì¹ç¤Ë-1¤òÊÖ¤·¤Æ¤¤¤Þ¤¹¤¬¡¢É¬Íפ˱þ¤¸¤ÆÊ̤Υǥե©¥ë¥ÈÃͤËÊѹ¹¤¹¤ë¤³¤È¤¬²Äǽ¤Ç¤¹¡£
¡¡¤³¤Îµ½Ò¤Ï¡¢¸µ¤Îʸ»úÎó¤¬Àµ¤·¤¤À°¿ôÃͤò»ý¤Ã¤Æ¤¤¤ë¤³¤È¤¬³ÎÄꤷ¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ë¤ª¤¤¤ÆºÎÍѤ¹¤ë¤È¤è¤¤¤Ç¤·¤ç¤¦¡£
int.Parse¤Ï²òÀÏ(ÊÑ´¹)¤Ë¼ºÇÔ¤¹¤ë¤ÈÎã³°¤ò¥¹¥í¡¼¤·¤Þ¤¹¤¬¡¢int.TryParse¤ÏÎã³°¤ò¥¹¥í¡¼¤»¤º¤Ëñ¤Ëfalse¤òÊÖ¤¹¤¿¤á¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤Æ½èÍý¤¬Ää»ß¤¹¤ë¤³¤È¤ò̤Á³¤ËËɤ°¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
¡ã»²¹Í¥µ¥¤¥È¡ä
MicroSoft
ʸ»úÎó¤ò¿ôÃͤËÊÑ´¹¤¹¤ëÊýË¡ (C# ¥×¥í¥°¥é¥ß¥ó¥° ¥¬¥¤¥É)
MicroSoft
Int32.TryParse ¥á¥½¥Ã¥É
MicroSoft
Int32.Parse ¥á¥½¥Ã¥É
¥µ¥à¥é¥¤¥¨¥ó¥¸¥Ë¥¢¥Ö¥í¥°
¡ÚC#ÆþÌç¡Ûʸ»úÎó¤ò¿ôÃͤˡ¢¿ôÃͤòʸ»úÎó¤ËÊÑ´¹¤¹¤ëÊýË¡
¡¡¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤ò¹Ô¤Ã¤¿¤é¡¢¤¤¤Þ¤Þ¤Ç¤ÈƱ¤¸¤è¤¦¤Ë½èÍý¤¬Æ°¤¯¤«¤É¤¦¤«¤ò³Îǧ¤·¤Æ¤ª¤¤¤Æ¤¯¤À¤µ¤¤¡£
¡¡º£²ó¼ÂÁõ¤·¤Æ¤¤¤ë List ¤Î¥»¡¼¥Ö¡¦¥í¡¼¥É¤Îµ¡Ç½¤ò±þÍѤ·¤Æ¡¢¤â¤¦£±¤Ä¤Î List<int> ÊÑ¿ô¤Ç¤¢¤ë¥¯¥ê¥¢¤·¤¿¥¹¥Æ¡¼¥¸¤Î¾ðÊó¤Ë¤Ä¤¤¤Æ¤â
¥»¡¼¥Ö¡¦¥í¡¼¥É¤¹¤ëµ¡Ç½¤Î¼ÂÁõ¤ËÄ©À路¤Æ¤ß¤Æ¤¯¤À¤µ¤¤¡£¼ÂÁõÊýË¡¤Ï¡¢¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°Á°¸å¤Î¤É¤Á¤é¤Î½èÍý¤Ç¤â¹½¤¤¤Þ¤»¤ó¤¬¡¢
½ÐÍè¤ë¤Ê¤é¤Ð¡¢¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°Á°¤Î½èÍý¤ò¼ÂÁõ¤·¤Æ¤«¤é¡¢¼«Ê¬¤Ç¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤·¤Æ¡¢½èÍý¤ò¥¹¥Þ¡¼¥È¤Ëµ½Ò¤¹¤ëÊýË¡¤ò³Ø½¬¤·¤Æ¤ß¤Æ¤¯¤À¤µ¤¤¡£
¡¡ºÇ½é¤«¤é¥¹¥Þ¡¼¥È¤Ê½èÍý¤ò½ñ¤¯¤³¤È¤ÏÆñ¤·¤¤¤Î¤Ç¡¢£±¤Ä¤º¤Ä½çÈ֤ˡ¢¼ÂÁõ¤È¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤ò·«¤êÊÖ¤·¤Æ³Ð¤¨¤Æ¤¤¤¯¤È¤¤¤¤¤Ç¤·¤ç¤¦¡£
¡¡¼ÂÁõ¤·¤Æ¥Ç¥Ð¥Ã¥°¤¬½ªÎ»¤·¤¿¤é¡¢·ÀÌó¥¥ã¥é¤Î¥»¡¼¥Ö¡¦¥í¡¼¥É¤ÈƱ¤¸¤è¤¦¤Ë¡¢¤É¤Î¥¿¥¤¥ß¥ó¥°¤Ç¥»¡¼¥Ö¡¦¥í¡¼¥É¤ò¹Ô¤¦¤Ù¤¤«¤ò¹Í¤¨¤Æ¡¢½èÍý¤Î¼ÂÁõ¤Ë¤âÄ©À路¤Æ¤ß¤Æ¤¯¤À¤µ¤¤¡£
¡¡°Ê¾å¤Ç¤³¤Î¼ê½ç¤Ï½ªÎ»¤Ç¤¹¡£
¡¡¼¡¤Ï¡¡È¯Å¸£±£¹¡¡¡¼¥»¡¼¥Ö¡¦¥í¡¼¥Éµ¡Ç½¤Î¼ÂÁõ£¡Ý¡¡¤Ç¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯