简体   繁体   中英

Move part of string to another string

Maybe I don't have the right keywords but I can't seem to find how to do it.

Let's say I have these two string :

firstString = "I am a string";

secondString = "I am a long";

Is there a method that would allow me to move part of string 1 to string 2 ?

Move, not copy.

The final results would be :

firstString = "I am a"

secondString = "I am a long string"

The Problem

I have a string that contains a lot of characters. I want to send this string to SQLServer but the function that receives it can't hold more than 8000 char. So I need to send a request every 8000 characters.

  • Check if String1 is longer than 8000 Char
  • If it is, take the first 8000 Char and MOVE them into String2
  • Insert String2 into SQL
  • Repeat
  • If it's lenght is smaller than 8000 Char, send String 1 to SQL

Strings are immutable so what you are really doing is reassigning part of firstString to itself and assigning the other part concatenated to the end of secondString . There is no built in method to achieve this, but the code is pretty simple

secondString += firstString.Substring(6);
firstString = firstString.Substring(0,6);

Is there a method that would allow me to move part of string 1 to string 2 ? Move, not copy.

Since .NET strings are immutable, they cannot support methods with "move" semantic. Every modification of a string requires creation of a new object, entailing copying .

EDIT (in response to the edit of the question) It looks like your problem has to do with splitting the string at 8K characters, not necessarily moving parts of the string. In this case, you could use this simple code to pass parts of the string to SQL:

string string1 = GetReallyLongString();
const int sqlMax = 8000;
while (true) {
    if (string1.Length > sqlMax) {
        SendToSql(string1.Substring(0, sqlMax));
        string1 = string1.Substring(sqlMax);
    } else {
        SendToSql(string1);
        break;
    }
}

Here is a quick demo on ideone .

As strings are immutable, you can't change them. You create new strings with parts from the original strings:

firstString = "I am a string";
secondString = "I am a long";

// concatenate second string with part from first string
secondSring = secondString + firstString.Substring(6);

// create a new string from part of the first string
firstString = firstString.Substring(0, 6);

Inspired by dasblinkenlight's solution, but using yield to make a static method, and using as few substrings as possible to reduce memory usage.

using System;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        string string1 = "quick brown fox jumps over the lazy dog";
        foreach (var strSection in string1.SplitInto(8))
            Console.WriteLine("'{0}'", strSection);
    }
}
public static class MyExtensions
{
    public static IEnumerable<string> SplitInto(this string value, int size)
    {
        for (int i = 0; i < value.Length; i += size)
        {
            if (i + size <= value.Length)
                yield return value.Substring(i, size);
            else
                yield return value.Substring(i);
        }
    }
}

See it run at Ideone .

Many ways to do that:

var partofstring = firstString.Substring(10, 14);
firstString = firstString.Replace(partofstring, "");

secondString += partofstring;

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