0

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
5
  • I almost want to downvote this because it was initially asking something completely different than your real issue. However, you cleared it up, so it's ok now. Commented May 16, 2013 at 14:56
  • @TimS. Well as you should know by now, we try to keep it as general as possible here on SO. No one cares about my specific problem when what I actually want to know is if you can move a part from a string to another. Knowing this is very helpful. Knowing how to solve my particular sql problem is not. At all. Commented May 16, 2013 at 15:37
  • But moving part of a string from one string to another is quite a different problem than splitting a string into several chunks. Commented May 16, 2013 at 19:02
  • @TimS. If I learn how to move part of a string to another, I know how to do the rest. Also, I would have used String.Split() but it woudn't work in my situation. Commented May 16, 2013 at 19:04
  • Knowing how to "move" (I use quotes because, being immutable, you're really creating new copies with the new values, but unless your strings are large enough to be a large memory drain, that's just semantics) a substring from one string to another is one way of solving your issue. I would argue that it's a confusing, and thus poor, way. You are, as I understand it, "moving" a substring, always of a fixed length, from the first portion of the string, "into" the empty string, which becomes your second string. Commented May 16, 2013 at 19:10

5 Answers 5

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice I think that's exactly what I need :) !
3

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.

Comments

2

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);

3 Comments

@PhaDaPhunk: You can't move a part of a string to another string, as strings are immutable. You could put it in a StringBuilder, which actually allows modification of the content, but that is not any more efficient than creating new strings.
Ok I got that. Now I could simulate a move. By using substring. right ? Thanks :)
@PhaDaPhunk: If you want to divide a string into 8000 character chunks, you don't even have to "move" the chunk from the original string, you can just copy one chunk at a time and leave the original string unchanged. That would be the most efficient way to get the chunks.
0

Many ways to do that:

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

secondString += partofstring;

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.