基本上我要做的是拿一个字符串
string test = "hello";
然后将其转换为数组:
string[] testing = { "h", "he", "hel", "hell", "hello" };
这可能吗?
答案 0 :(得分:7)
尝试使用 Linq :
assertThat(BigDecimal.ONE,
is(both(not(nullValue(BigDecimal.class)))
.and(not(comparesEqualTo(BigDecimal.ZERO)))));
测试:
string test = "hello";
string[] testing = Enumerable
.Range(1, test.Length)
.Select(length => test.Substring(0, length))
.ToArray();
答案 1 :(得分:1)
您也可以这样做:
List<string> list = new List<string>();
for(int i = 1; i <= hello.Length; i++) {
list.Add(hello.Substring(0,i));
}
Console.WriteLine(string.Join(", ", list.ToArray()));
答案 2 :(得分:1)
我推荐Dmitry的LINQ版本,但是如果你想要一个像你原来的问题那样使用数组的简单版本:
string input = "hello";
string[] output = new string[input.Length];
for( int i = 0; i < test.Length; ++i )
{
output[i] = test.Substring( 0, i + 1 );
}
答案 3 :(得分:0)
string test = "hello";
string[] arr = new string[] {test.Substring(0,1), test.Substring(0,2), test.Substring(0,3), test.Substring(0,4), test.Substring(0,5)};
答案 4 :(得分:0)
是的,你使用Linq。
string test = "hello";
List<string> lst = new List<string>();
int charCount = 1;
while (charCount <= test.Length)
{
lst.Add(string.Join("", test.Take(charCount).ToArray()));
charCount++;
}
string[] testing = lst.ToArray();