- Home /
 
Split String with ':' from a Text Asset
Evening all,
This problem is giving me a toothache! I have the below script which works wonderfully (almost)
 public class DialogueManager : MonoBehaviour
 {
 
     public List<string> textArray;
 
     public bool display;
     public TextMeshProUGUI dialogueText;
 
     public int[] rowsToReadFrom;
 
     public string fileName;
 
     public TextAsset textAsset;
 
 
     // Start is called before the first frame update
     void Start()
     {
         textAsset = Resources.Load("TextFiles/" + fileName) as TextAsset;
         dialogueText = GetComponent<TextMeshProUGUI>();
         textArray = textAsset.text.Split('\n').ToList();
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     public void InfoDisplay(int messageToDisplay)
     {
         textArray[messageToDisplay].Split(':');
         dialogueText.text = textArray[messageToDisplay];
         
     }
 }
 
               However my textAsset file has on line[0] a string of text that I want to display in 1 text field but split onto two lines so it will essentially read:
"Hi, my name is something"
"Press to make me shrink!"
The above text is on 1 line within my text file and I can't split over two otherwise it crates two entries in my list.
I have been going round in circles with string.Split in various formats but to no avail. Please help!!
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Hellium · Sep 03, 2020 at 11:15 PM
  public void InfoDisplay(int messageToDisplay)
  {
      textArray[messageToDisplay] = textArray[messageToDisplay].Replace(':', '\n');
      dialogueText.text = textArray[messageToDisplay];         
  }
 
               But I don't advise using : as separator unless you have the guarantee you won't use this character for anything but this. 
Your answer