facebook twitter hatena line email

「Unity/Csharp/文字操作」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(分割(改行))
(文字列リテラル)
(同じ利用者による、間の18版が非表示)
行13: 行13:
 
     // hit
 
     // hit
 
  }
 
  }
 +
見つけた場合は 0 から始まるインデックスを返す。見つからなかった場合は-1
  
 
==分割==
 
==分割==
行23: 行24:
 
  using System;
 
  using System;
 
  string str = "test\r\ntest\r\ntest";
 
  string str = "test\r\ntest\r\ntest";
  string[] lines = str.Split(new []{"\r\n"}, StringSplitOptions.None);
+
  string[] lines = str.Split(new []{"\r\n", "\r", "\n"}, StringSplitOptions.None);
  
 
==小文字を大文字へ==
 
==小文字を大文字へ==
行30: 行31:
 
==大文字を小文字へ==
 
==大文字を小文字へ==
 
  name.ToLower();
 
  name.ToLower();
 +
 +
==.区切りの一番最後の文字列を取得==
 +
jp.co.example.hogehogeのhogehogeを取得する
 +
int startindex = className.LastIndexOf('.') + 1;
 +
string name = className.Substring(startindex, className.Length - startindex);
 +
 +
==ヒアドキュメント==
 +
string str = @"hogehoge
 +
hogehoge";
 +
@をつけると改行があっても大丈夫
 +
 +
==ある文字から最後までを抜き出す==
 +
例:{"があるところから最後までを抜き出す
 +
json = json.Substring(json.IndexOf("{\""), json.Length - json.IndexOf("{\""));
 +
 +
==初回検索の文字だけ置換==
 +
using System;
 +
using System.Text.RegularExpressions;
 +
string str = "hogehoge";
 +
string searchWord = "hoge";
 +
string after = "piyo";
 +
Regex re = new Regex(searchWord); 
 +
ret = re.Replace(str, after, 1);
 +
Debug.Log("ret=" + ret); // piyohoge
 +
 +
http://noriok.hatenadiary.jp/entry/2015/09/21/184424
 +
 +
==初回検索の文字から検索==
 +
Match matche = Regex.Match("hogepiyo", "^hoge");
 +
if (matche.Success)
 +
{
 +
    Debug.Log(matche.Value); // hoge
 +
}
 +
 +
==正規表現マッチ==
 +
string str = "hoge a456 piyo";
 +
Match matche = Regex.Match(str, "a[0-9]+");
 +
if (matche.Success) {
 +
    Debug.Log(matche.Value); // a456
 +
}
 +
 +
==a-z判定==
 +
<pre>
 +
if (StrUtil.IsAlphabet(name)) {
 +
}
 +
public class StrUtil
 +
{
 +
    // アルファベットのみ
 +
    public static bool IsAlphabet(string name)
 +
    {
 +
        return !Regex.IsMatch(name, @"[^a-zA-Z]");
 +
    }
 +
    // 数字のみ
 +
    public static bool IsNum(string name)
 +
    {
 +
        return !Regex.IsMatch(name, @"[^0-9]");
 +
    }
 +
}
 +
</pre>
 +
参考:http://increment.hatenablog.com/entry/2015/08/21/060626
 +
 +
==半角判定==
 +
<pre>
 +
using System.Text;
 +
public class StrUtil
 +
{
 +
    // 最初の1文字だけ判定
 +
    public static bool IsHankaku(string str)
 +
    {
 +
        char[] chars = str.ToCharArray();
 +
        Encoding chk = Encoding.GetEncoding("shift_jis"); // shift_jisだと2でutf-8だと3
 +
        if (chars.Length >= 1) {
 +
            if (chk.GetByteCount(chars, 0, 1) == 1) {
 +
                return true; // 1byte
 +
            }
 +
        }
 +
        return false; // 2byte
 +
    }
 +
}
 +
</pre>
 +
Android(iosも?)の場合は半角でも2となるのでダメっぽい。
 +
 +
==全角を半角へ==
 +
<pre>
 +
    // 1文字を半角に
 +
    static public string ConvertToHalfWidth(string fullWidthStr)
 +
    {
 +
        if (fullWidthStr.Length == 0) {
 +
            return fullWidthStr;
 +
        }
 +
        string halfWidthStr = null;
 +
        const int ConvertionConstant = 65248;
 +
        halfWidthStr += (char)(fullWidthStr[0] - ConvertionConstant);
 +
        return halfWidthStr;
 +
    }
 +
</pre>
 +
参照:http://koma2961.hatenablog.com/entry/2016/02/24/051156
 +
 +
==文字列リテラル==
 +
%sとかの置換のやり方
 +
string message = string.Format("({0}個以上は{1}をしてください。)", i, move);

2020年5月19日 (火) 13:31時点における版

文字の長さ

String name = "abcdef";
name.Length; // 6

文字の切り取り

String name = "abcdef";
name = name.Substring (name.Length - 2, 2); // ef

検索

if (name.IndexOf("検索文字列") == -1) {
    // no hit
} else {
    // hit
}

見つけた場合は 0 から始まるインデックスを返す。見つからなかった場合は-1

分割

name = "1234:2345";
string[] names = name.Split(":"[0]);
Debug.Log(names[0]); // 1234
Debug.Log(names[1]); // 2345

分割(改行)

using System;
string str = "test\r\ntest\r\ntest";
string[] lines = str.Split(new []{"\r\n", "\r", "\n"}, StringSplitOptions.None);

小文字を大文字へ

name.ToUpper();

大文字を小文字へ

name.ToLower();

.区切りの一番最後の文字列を取得

jp.co.example.hogehogeのhogehogeを取得する

int startindex = className.LastIndexOf('.') + 1;
string name = className.Substring(startindex, className.Length - startindex);

ヒアドキュメント

string str = @"hogehoge
hogehoge";

@をつけると改行があっても大丈夫

ある文字から最後までを抜き出す

例:{"があるところから最後までを抜き出す

json = json.Substring(json.IndexOf("{\""), json.Length - json.IndexOf("{\""));

初回検索の文字だけ置換

using System;
using System.Text.RegularExpressions;
string str = "hogehoge";
string searchWord = "hoge";
string after = "piyo";
Regex re = new Regex(searchWord); 
ret = re.Replace(str, after, 1);
Debug.Log("ret=" + ret); // piyohoge

http://noriok.hatenadiary.jp/entry/2015/09/21/184424

初回検索の文字から検索

Match matche = Regex.Match("hogepiyo", "^hoge");
if (matche.Success)
{
    Debug.Log(matche.Value); // hoge
}

正規表現マッチ

string str = "hoge a456 piyo";
Match matche = Regex.Match(str, "a[0-9]+");
if (matche.Success) {
    Debug.Log(matche.Value); // a456
}

a-z判定

if (StrUtil.IsAlphabet(name)) {
}
public class StrUtil
{
    // アルファベットのみ
    public static bool IsAlphabet(string name)
    {
        return !Regex.IsMatch(name, @"[^a-zA-Z]");
    }
    // 数字のみ
    public static bool IsNum(string name)
    {
        return !Regex.IsMatch(name, @"[^0-9]");
    }
}

参考:http://increment.hatenablog.com/entry/2015/08/21/060626

半角判定

using System.Text;
public class StrUtil
{
    // 最初の1文字だけ判定
    public static bool IsHankaku(string str)
    {
        char[] chars = str.ToCharArray();
        Encoding chk = Encoding.GetEncoding("shift_jis"); // shift_jisだと2でutf-8だと3
        if (chars.Length >= 1) {
            if (chk.GetByteCount(chars, 0, 1) == 1) {
                return true; // 1byte
            }
        }
        return false; // 2byte
    }
}

Android(iosも?)の場合は半角でも2となるのでダメっぽい。

全角を半角へ

    // 1文字を半角に
    static public string ConvertToHalfWidth(string fullWidthStr)
    {
        if (fullWidthStr.Length == 0) {
            return fullWidthStr;
        }
        string halfWidthStr = null;
        const int ConvertionConstant = 65248;
        halfWidthStr += (char)(fullWidthStr[0] - ConvertionConstant);
        return halfWidthStr;
    }

参照:http://koma2961.hatenablog.com/entry/2016/02/24/051156

文字列リテラル

%sとかの置換のやり方

string message = string.Format("({0}個以上は{1}をしてください。)", i, move);