String split by length

i have a string with 75 characters , i want to take 25 characters to another string how can i do that?

1 Answer

1

Use String.SubString.

var bar = foo.Substring(0, 25);

bar will contain the first 25 characters of foo.

var bar = foo.Substring(index, length);

bar will contain length characters from foo, starting from index.


If you want to create a function that chops a string by length and return an array of those strings, here's one in C#:

public static string[] Chop(string value, int length)
{
    int strLength = value.Length;
    int strCount = (strLength + length - 1) / length;
    string[] result = new string[strCount];
    for (int i = 0; i < strCount; ++i)
    {
        result _= value.Substring(i * length, Mathf.Min(length, strLength));_
 *strLength -= length;*
 *}*
 *return result;*
*}*
*```*
*<p>Example usage:</p>*
*```*
*string bar = "this is an incredible long string that will be chopped down";*
*string[] foo = Chop(bar, 10);*
*foreach(string f in foo)*
 *Debug.Log(string.Format("[{0}]", f));*
*```*
*<hr>*
*<p>If you want you can make it an extension to string.</p>*
*```*
*public static class StringChop*
*{*
 *public static string[] Chop(this string value, int length)*
 *{*
 *int strLength = value.Length;*
 *int strCount = (strLength + length - 1) / length;*
 *string[] result = new string[strCount];*
 *for (int i = 0; i < strCount; ++i)*
 *{*
 <em>result _= value.Substring(i * length, Mathf.Min(length, strLength));_</em>
 _*strLength -= length;*_
 _*}*_
 _*return result;*_
 _*}*_
_*}*_
_*```*_
_*<p>Then you can use it like this:</p>*_
_*```*_
_*string bar = "this is an incredible long string that will be chopped down";*_
_*string[] foo = bar.Chop(10);*_
_*foreach(string f in foo)*_
 _*Debug.Log(string.Format("[{0}]", f));*_
_*```*_

No. It ALSO fires when just losing focus.