0

I want to use a StringBuilder to merge a couple of strings, and then give them to EditorGUIUtility.systemCopyBuffer in UnityEngine. One of the strings contains newline characters in itself, but they are not present when I paste the contents of the EditorGUIUtility.systemCopyBuffer. I cannot add the newline characters myself, as I don't know how many there are or where.

// This string is in my real code unknown to me.
// I also don't know the format of the newline characters.
string description = "A long description\nwith multiple lines.";

var builder = new StringBuilder();
builder.Append("Name: ");
builder.AppendLine(someObject.Name);
builder.Append("Description: ");
builder.AppendLine(description);
Debug.Log(builder.ToString());
EditorGUIUtility.systemCopyBuffer = builder.ToString();

The expected output, which is printed by Debug.Log in the Unity Console, is:

Name: A name
Description: A long description
with multiple lines.

So StringBuilder can handle it. But when I paste the text into e.g. notepad, it looks like this:

Name: A name
Description: A long descriptionwith multiple lines.

So the system copy buffer can handle the newlines that StringBuilder adds, but not the ones present in the original string. Can I make EditorGUIUtility.systemCopyBuffer include the newlines from the original string somehow?

Helena
  • 1,041
  • 2
  • 12
  • 24

1 Answers1

0

There are different ways to enter newline in different OS. For Windows - \r\n, Form Mac - \r, For Unix - \n and Linux - \n

Use Environment.NewLine instead of \n.

var exp= new Regex(@"(?:\r\n|[\r\n])+");

var general = exp.Replace(test, Environment.NewLine);

Ghost Developer
  • 1,283
  • 1
  • 10
  • 18
  • Sorry if I was unclear. Unfortunately, I don't enter the newlines, they come from other systems. People can enter these descriptions on multiple different operating systems, so I never know the nature of the newlines. – Helena Jan 15 '17 at 09:51
  • There are different ways to enter newline in different OS. Windows is \r\n, Mac is \r, Unix and Linux are \n – Ghost Developer Jan 15 '17 at 10:31
  • Yes, I know. The string I get that I want to handle could contain them all. So, for instance, I want someone to be able to enter the content of the string "description" on their Mac, and I should be able to read it and keep the line endings on Windows in the way described above. Or any other OS combination. StringBuilder seems to be able to handle it. Perhaps Unity's EditorGUIUtility.systemCopyBuffer simply isn't smart enough :) – Helena Jan 16 '17 at 06:42