Home > Mobile >  Copy text without new line
Copy text without new line

Time:12-08

I have two text mesh pros and I am trying to copy from one to another. The first text mesh pro looks like this:

Computer.
CPU, GPU, HDD.
Graphics Card Nvidia.

Now I want to copy this to my second text mesh pro but without new lines. That is, the final text should be:

Computer. CPU, GPU, HDD. Graphics Card Nvidia.

How do I do this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class CopyMessage : MonoBehaviour
{
    public TextMeshProUGUI text1;
    public TextMeshProUGUI text2;

    // Start is called before the first frame update
    void Start()
    {
        text2.text = text1.text;
    }

CodePudding user response:

Try to replace newlines with blank or whitespace like this:

text2.text = text1.text.Replace("\n","");;

CodePudding user response:

You want to replace new line with a blank. Take a look at What would be the fastest way to remove Newlines from a String in C#?

in your case this would be something like this

text2.text = text1.text.Replace(Environment.NewLine, " ");
  • Related