「Unity/Csharp/文字操作」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→初回検索の文字から検索) |
(→複数正規表現から置換) |
||
| (同じ利用者による、間の42版が非表示) | |||
| 行1: | 行1: | ||
| + | ==文字判定== | ||
| + | <pre> | ||
| + | string url = ""; | ||
| + | if (string.IsNullOrEmpty(url)) | ||
| + | { | ||
| + | Debug.Log("empty!!"); | ||
| + | } | ||
| + | </pre> | ||
| + | 空文字はtrue判定される | ||
| + | |||
==文字の長さ== | ==文字の長さ== | ||
String name = "abcdef"; | String name = "abcdef"; | ||
| 行5: | 行15: | ||
==文字の切り取り== | ==文字の切り取り== | ||
String name = "abcdef"; | String name = "abcdef"; | ||
| − | name = name.Substring (name.Length - 2, 2); // ef | + | name = name.Substring(1, 2); // bc |
| + | name = name.Substring(name.Length - 2, 2); // ef | ||
| + | |||
| + | ==文字繰り返し== | ||
| + | string filter = new string('o', 10); // oooooooooo | ||
==検索== | ==検索== | ||
| − | if (name.IndexOf("検索文字列") | + | if (name.IndexOf("検索文字列") > -1) { |
| − | + | ||
| − | + | ||
// hit | // hit | ||
| + | } else { | ||
| + | // no hit | ||
} | } | ||
| + | 見つけた場合は 0 から始まるインデックスを返す。見つからなかった場合は-1 | ||
| + | ===検索(指定文字数移行)=== | ||
| + | <pre> | ||
| + | string name = "あいういお"; | ||
| + | int index = name.IndexOf("い", 2); // 3文字目以降で検索 | ||
| + | </pre> | ||
| + | 2つ目の"い"は、4番目だったので、-1して、indexには3が入る | ||
| − | ==分割== | + | ==検索その2== |
| − | + | <pre> | |
| − | + | string str = "あいうえお"; | |
| − | + | string target = "いうえか"; | |
| − | + | // 含まれる | |
| + | if (str.Contains(target)) | ||
| + | { | ||
| + | Debug.Log("match"); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Debug.Log("no match"); | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | ==分割(配列へ)== | ||
| + | <pre> | ||
| + | name = "1234:2345"; | ||
| + | string[] names = name.Split(":"[0]); | ||
| + | Debug.Log(names[0]); // 1234 | ||
| + | Debug.Log(names[1]); // 2345 | ||
| + | </pre> | ||
| + | |||
| + | ==分割(Listへ)== | ||
| + | <pre> | ||
| + | name = "1234:2345"; | ||
| + | List<int> result = name | ||
| + | .Split(':') | ||
| + | .Select(a => int.Parse(a)) | ||
| + | .ToList(); | ||
| + | </pre> | ||
==分割(改行)== | ==分割(改行)== | ||
| 行25: | 行72: | ||
string[] lines = str.Split(new []{"\r\n", "\r", "\n"}, StringSplitOptions.None); | string[] lines = str.Split(new []{"\r\n", "\r", "\n"}, StringSplitOptions.None); | ||
| + | ==分割(1文字ごと)== | ||
| + | foreach (var item in str.Select((n, index) => new { n, index })) | ||
| + | { | ||
| + | Debug.Log(item.n); | ||
| + | } | ||
==小文字を大文字へ== | ==小文字を大文字へ== | ||
name.ToUpper(); | name.ToUpper(); | ||
| 行31: | 行83: | ||
name.ToLower(); | name.ToLower(); | ||
| − | ==.区切りの一番最後の文字列を取得== | + | ==最初の文字だけ大文字へ== |
| + | System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str); | ||
| + | |||
| + | ==最初の文字だけ大文字で他は維持== | ||
| + | string pageUpper = (page.Length > 0 ) ? page.Substring(0, 1).ToUpper() + page.Substring(1, page.Length - 1) : ""; | ||
| + | |||
| + | =="."区切りの一番最後の文字列を取得== | ||
jp.co.example.hogehogeのhogehogeを取得する | jp.co.example.hogehogeのhogehogeを取得する | ||
int startindex = className.LastIndexOf('.') + 1; | int startindex = className.LastIndexOf('.') + 1; | ||
| 行44: | 行102: | ||
例:{"があるところから最後までを抜き出す | 例:{"があるところから最後までを抜き出す | ||
json = json.Substring(json.IndexOf("{\""), json.Length - json.IndexOf("{\"")); | json = json.Substring(json.IndexOf("{\""), json.Length - json.IndexOf("{\"")); | ||
| + | |||
| + | ==置換== | ||
| + | <pre> | ||
| + | string str = "my name is taro"; | ||
| + | Debug.Log(str.Replace("taro", "jiro")); // my name is jiro | ||
| + | </pre> | ||
| + | |||
| + | ==結合== | ||
| + | <pre> | ||
| + | string username = "jiro"; | ||
| + | Debug.Log("my name is " + username + "."); | ||
| + | </pre> | ||
| + | |||
| + | 基本こういう結合は、文字列置換式を使う。 | ||
| + | |||
| + | 参考:https://qiita.com/Nossa/items/c2226232b31d7665267f | ||
| + | ===文字列補間式=== | ||
| + | <pre> | ||
| + | string username = "jiro"; | ||
| + | Debug.Log($"my name is {username}."); | ||
| + | </pre> | ||
| + | |||
| + | ===string.Format=== | ||
| + | %sとかの置換のやり方 | ||
| + | string message = string.Format("({0}個以上は{1}をしてください。)", i, move); | ||
==初回検索の文字だけ置換== | ==初回検索の文字だけ置換== | ||
| 行58: | 行141: | ||
==初回検索の文字から検索== | ==初回検索の文字から検索== | ||
| − | Match | + | Match match = Regex.Match("hogepiyo", "^hoge"); |
| − | if ( | + | if (match.Success) |
{ | { | ||
| − | Debug.Log( | + | Debug.Log(match.Value); // hoge |
} | } | ||
| − | == | + | ==複数正規表現から置換== |
| − | + | <pre> | |
| − | + | MatchCollection matches = Regex.Matches(str, @"([0-9]{4})/([0-9]{1,2})/([0-9]{1,2}) "); | |
| − | + | foreach (Match match in matches) | |
| + | { | ||
| + | Debug.Log(match.Value); | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | ==半角判定== | ||
| + | <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 | ||
| + | |||
| + | ==数字のフォーマットの置換== | ||
| + | String.Format("数字{0:###.#}", 4); | ||
| + | // 数字004.0 | ||
| + | String.Format("数字{0:f}", 5); | ||
| + | // 数字5.00 | ||
| + | |||
| + | ==右から何文字抜き出し== | ||
| + | <pre> | ||
| + | public static string Right(string str, int len) | ||
| + | { | ||
| + | if (str.Length <= len) | ||
| + | { | ||
| + | return str; | ||
| + | } | ||
| + | return str.Substring(str.Length - len, len); | ||
| + | } | ||
| + | </pre> | ||
| + | 参考:https://dobon.net/vb/dotnet/vb2cs/mid.html | ||
| + | |||
| + | ==文字から数字だけ抜き出し== | ||
| + | <pre> | ||
| + | using System.Text.RegularExpressions; | ||
| + | string str = Regex.Replace (name, @"[^0-9]", ""); | ||
| + | Debug.Log (str); | ||
| + | </pre> | ||
| + | |||
| + | ==キャメルケースをスネークケースに== | ||
| + | <pre> | ||
| + | using System.Text.RegularExpressions; | ||
| + | string snakeCase = Regex.Replace("HogeHoge", "([a-z0-9])([A-Z])", "$1_$2").ToLower(); // hoge_hoge | ||
| + | </pre> | ||
2026年3月11日 (水) 05:14時点における最新版
目次
- 1 文字判定
- 2 文字の長さ
- 3 文字の切り取り
- 4 文字繰り返し
- 5 検索
- 6 検索その2
- 7 分割(配列へ)
- 8 分割(Listへ)
- 9 分割(改行)
- 10 分割(1文字ごと)
- 11 小文字を大文字へ
- 12 大文字を小文字へ
- 13 最初の文字だけ大文字へ
- 14 最初の文字だけ大文字で他は維持
- 15 "."区切りの一番最後の文字列を取得
- 16 ヒアドキュメント
- 17 ある文字から最後までを抜き出す
- 18 置換
- 19 結合
- 20 初回検索の文字だけ置換
- 21 初回検索の文字から検索
- 22 複数正規表現から置換
- 23 半角判定
- 24 全角を半角へ
- 25 数字のフォーマットの置換
- 26 右から何文字抜き出し
- 27 文字から数字だけ抜き出し
- 28 キャメルケースをスネークケースに
文字判定
string url = "";
if (string.IsNullOrEmpty(url))
{
Debug.Log("empty!!");
}
空文字はtrue判定される
文字の長さ
String name = "abcdef"; name.Length; // 6
文字の切り取り
String name = "abcdef"; name = name.Substring(1, 2); // bc name = name.Substring(name.Length - 2, 2); // ef
文字繰り返し
string filter = new string('o', 10); // oooooooooo
検索
if (name.IndexOf("検索文字列") > -1) {
// hit
} else {
// no hit
}
見つけた場合は 0 から始まるインデックスを返す。見つからなかった場合は-1
検索(指定文字数移行)
string name = "あいういお";
int index = name.IndexOf("い", 2); // 3文字目以降で検索
2つ目の"い"は、4番目だったので、-1して、indexには3が入る
検索その2
string str = "あいうえお";
string target = "いうえか";
// 含まれる
if (str.Contains(target))
{
Debug.Log("match");
}
else
{
Debug.Log("no match");
}
分割(配列へ)
name = "1234:2345";
string[] names = name.Split(":"[0]);
Debug.Log(names[0]); // 1234
Debug.Log(names[1]); // 2345
分割(Listへ)
name = "1234:2345";
List<int> result = name
.Split(':')
.Select(a => int.Parse(a))
.ToList();
分割(改行)
using System;
string str = "test\r\ntest\r\ntest";
string[] lines = str.Split(new []{"\r\n", "\r", "\n"}, StringSplitOptions.None);
分割(1文字ごと)
foreach (var item in str.Select((n, index) => new { n, index }))
{
Debug.Log(item.n);
}
小文字を大文字へ
name.ToUpper();
大文字を小文字へ
name.ToLower();
最初の文字だけ大文字へ
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str);
最初の文字だけ大文字で他は維持
string pageUpper = (page.Length > 0 ) ? page.Substring(0, 1).ToUpper() + page.Substring(1, page.Length - 1) : "";
"."区切りの一番最後の文字列を取得
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("{\""));
置換
string str = "my name is taro";
Debug.Log(str.Replace("taro", "jiro")); // my name is jiro
結合
string username = "jiro";
Debug.Log("my name is " + username + ".");
基本こういう結合は、文字列置換式を使う。
参考:https://qiita.com/Nossa/items/c2226232b31d7665267f
文字列補間式
string username = "jiro";
Debug.Log($"my name is {username}.");
string.Format
%sとかの置換のやり方
string message = string.Format("({0}個以上は{1}をしてください。)", i, move);
初回検索の文字だけ置換
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 match = Regex.Match("hogepiyo", "^hoge");
if (match.Success)
{
Debug.Log(match.Value); // hoge
}
複数正規表現から置換
MatchCollection matches = Regex.Matches(str, @"([0-9]{4})/([0-9]{1,2})/([0-9]{1,2}) ");
foreach (Match match in matches)
{
Debug.Log(match.Value);
}
半角判定
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
数字のフォーマットの置換
String.Format("数字{0:###.#}", 4);
// 数字004.0
String.Format("数字{0:f}", 5);
// 数字5.00
右から何文字抜き出し
public static string Right(string str, int len)
{
if (str.Length <= len)
{
return str;
}
return str.Substring(str.Length - len, len);
}
参考:https://dobon.net/vb/dotnet/vb2cs/mid.html
文字から数字だけ抜き出し
using System.Text.RegularExpressions; string str = Regex.Replace (name, @"[^0-9]", ""); Debug.Log (str);
キャメルケースをスネークケースに
using System.Text.RegularExpressions;
string snakeCase = Regex.Replace("HogeHoge", "([a-z0-9])([A-Z])", "$1_$2").ToLower(); // hoge_hoge
