数字を3桁ごとにコンマで区切って表示したい場合、C#では文字列補間の補間式を使うと簡単にできるみたいです。
補完式の使い方は次のような感じです。
{<interpolationExpression>[,<alignment>][:<formatString>]}
サンプル
int price = 12345;
Console.WriteLine($"price: {price:#,0}");
実行結果
price: 12,345
ちなみに、string.Format
を使って実装したい場合は次のような感じになります。
サンプル
int price = 12345;
Console.WriteLine(string.Format("price: {0:#,0}", price));
実行結果
price: 12,345
リンク
$ – string interpolation – C# reference | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
関連記事
C#で配列char[]をstringに変換する方法
C#でcharの配列をstringに変換する方法を調べてみたところ、次の2つが使い易そうな感じでした。
new stringを使う方法
char[] array = { 'a', 'b', 'c', 'd', 'e' };
string s = new string(array);
Console.WriteLine(s);
実行結界
abcde
stri...
正規表現で文字列を分割したい場合はRegex.Split
C#で正規表現で文字列を分割したい場合はRegex.Splitが便利です。使い方は次のような感じです。
数字で分割するサンプル
using System;
using System.Text.RegularExpressions;
public class Example1
{
static public void Main ()
{
string pattern ...
C#の文字列補完
string.Format
string.Formatを使うと、次のような感じで変数の値を文字列に変換して出力できます。
コード
string name = "abc";
int value = 123;
Debug.Log(string.Format("name={0}, value={1}", name, value));
出力
name=abc, value=...
Enum.Parseの使い方
C#ではint.Parseやfloat.Parseを使って文字列型から数値型への変換ができます。これと同じような感じで文字列型からenum型に変換する方法がないかなと思って調べてみました。
ということで、SystemにあるEnum.ParseやEnum.TryParseを使うと良いみたいです。
使い方は次のような感じです。
enum Example { Apple, Orang...