简体   繁体   中英

Remove Middle Substring From a String in C#

I am practicing C#

I want to remove middle Substring from a char array and add it in fornt

Example 1

char array = " Fbh3dfbn8"

Substring 3 to 7 is removed and added at beginning:

char array = "3dfFbhbn8"

Example 2

char array = E5SS6SkGDE

Substring 4 to 7 is removed and added at beginning:

char array= S6SkE5SGDE

Basically I want to divide the string into 3 parts where length of 1st and 3rd substring is same.

if (charArray.ToString().Length % 2 != 0 && charArray.ToString().Length >= 3)
{
    string subStringCharArray = charArray.ToString().Substring(charArray.ToString().Length / 2 - 1, 3);
    charArray.ToString().IndexOf(subStringCharArray);
    string changedString = subStringCharArray + charArray.ToString();

} 

This is my code. At point string subStringCharArray inside if condition I always get mC as a output.But there is no mC` in my Char array.

Can any one help me or guide me to make this method.

Thanks

Use new String(charArray) in place of charArray.ToString()

Char[] charArray = new Char[]{'a', 'b', 'c'};

charArray.ToString() won't be the "abc" string. It will be 'System.Char[]' for any possible array.

Also

charArray.ToString().IndexOf(subStringCharArray);

won't change anything neither in array nor in string. It just returns the index of the substring - number. It does not modify anything.

You could try something like:

            Char[] charArray = new Char[] { '1', '2', '3', '4', '5', '6', '7' };
            String charArrayAsString = new String(charArray);

            String result = null;

            if (((charArrayAsString.Length % 2) != 0) &&
                 (charArrayAsString.Length >= 3))
            {
                Int32 middleBegins = charArrayAsString.Length / 2 - 1;
                Int32 middleLen = 3;

                result = charArrayAsString.Substring(middleBegins, middleLen) +
                    charArrayAsString.Substring(0, middleBegins) +
                    charArrayAsString.Substring(middleBegins + middleLen);                    
            }

            Console.WriteLine(result);

Divide through 3:

string str1 = "Fbh3dfbn8";
int partLength = str1.Length / 3;
string result = string.Format("{0}{1}{2}", 
    str1.Substring(partLength, str1.Length - (2 * partLength)),
    str1.Remove(partLength),
    str1.Substring(str1.Length - partLength));

If you insist on a char[] , it's easy enough to get a string from char[] via constructor or back to char[] via string.ToCharArray .

Try this:

string input = "E5SS6SkGDE";

char[] array = input.ToCharArray();

var result =
    array.Skip(3).Take(4)
        .Concat(array.Take(3))
        .Concat(array.Skip(7))
        .ToArray();

var output = new string(result);

The result I get is S6SkE5SGDE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM