13
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

配列の”中身”をいい感じに文字列化する拡張メソッド

Last updated at Posted at 2014-09-11

オブジェクトの配列をToString()しても "Hoge[]"とかあまり意味のない文字列が返ってくるので以下のような拡張メソッドを用意して、配列の各要素に対してToString()できるようにした。

さらに、string.Join() もメソッドチェーンで使えるようにしたら、コード書くときスムーズ。

Extensions.cs
static class Extensions {
	public static string[] ToStrings(this object[] objectArray) {
		return Array.ConvertAll<object, string>(objectArray, o => o.ToString()); 
	}
	
	public static string Join(this string[] stringArray, string separator = ",") {
		return string.Join (separator, stringArray);
	}
}

使用例

以下のように使える

example
Hoge[] objArray = GetSomeArray();
Debug.Log(objArray.ToStrings().Join());

13
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?