Nanashi-soft○プログラマ専用○Unityでゲーム開発○
string s = "文字列";※先頭のsは小文字です
string a = "文字列A"; string b = "文字列B"; string c; c = a + b; //文字列同士を足す c = a + "文字列C"; //変数に定数を足す c = "文字列C" + a; //定数に変数を足す
string a = "文字列A"; int b; b = a.Length;文字数が返ります。上の例だと4になります
string a = "文字列A"; string b; b = a.Substring(「開始位置」,「取り出す文字数」);開始位置は0から始まる
b = a.Substring(0, 2); //先頭から2文字→文字 b = a.Substring(1, 2); //2文字目から3文字→字列 b = a.Substring(1); //3文字目以降全部→字列A
string a = "文字列A\n文字列B"; string[] b; string c; b = a.Split("\n"[0]); //改行コードで分割 c = b[0]; //cには『文字列A』が入る
string a = "文字列A\n文字列B"; string[] b; string c; string[] KUGIRI = {"\r", "\n"}; //データの区切り文字 b = a.Split(KUGIRI); //KUGIRI変数内の各文字で分割 c = b[0]; //cには『文字列A』が入る
string a = "文字列A\n\n\n文字列B"; //間に改行が3つある string[] b; string c; b = a.Split("\n"[0], System.StringSplitOptions.RemoveEmptyEntries); //改行コードで分割。空欄は削除する//b[0]には『文字列A』
string a="文字列A"; if(a == "文字列A"){ //一致した }else{ //一致しなかった }・うまく比較できない場合はEqualsメソッドを使う
string a="文字列A"; if(a.Equals("文字列A")){ //一致した }else{ //一致しなかった }・宣言をしただけの変数との比較はできません
string a; if (a == null) { //x }↓こういうエラーが出ます
string a; int b=1; if (b == 1) { a = "初期化"; } if (a == null) { Debug.Log("true"); }※bが1でない場合が発生するかも知れないのでエラーになります
string a=null;
int a = 123; string b; b = "" + a;
float a = 123.45f; string b; b = "" + a;
string a = "123"; int b; try{ b = int.Parse(a); }catch{ //エラー時:文字列が整数で無い時(少数の時もエラーになる) }変換できない文字列の場合は以下のエラーになります
string a = "123.45"; float b; try{ b = float.Parse(a); }catch{ //エラー時:文字列が少数で無い時 }
string a = "123"; int b; if(int.TryParse(a, out b)){ //正常の時 //bに結果の123が入っている }else{ //エラー時:文字列が整数で無い時(少数の時もエラーになる) //bには常に0が入っている }
string a = "123.45"; float b; if(float.TryParse(a, out b)){ //正常の時 //bに結果の123.45が入っている }else{ //エラー時:文字列が少数で無い時 //bには常に0が入っている }
string a = "文字列A\n文字列B"; int b; b = a.IndexOf('\n');
if (a.IndexOf('\n') == -1) { //文字列が無かった } else { //文字列があった }
string debug = "デバッグ文字列"; Debug.Log(debug);※Consoleウィンドウは,メニューの Window→Consoleで表示されます