Home > Back-end >  C# array, String must be exactly one character long
C# array, String must be exactly one character long

Time:12-09

I'm pretty fluent but I'm not sure what I'm doing wrong here. I'm attempting to take data from a web output and parse it into an array by "br /"'s and spaces. Thanks for any help in advance. I'm getting the error "String must be exactly one character long" on the line string[] outputarray = ieoutput.Split(char.Parse("<br />")); towards the bottom. Thanks again.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class UserData : MonoBehaviour
{
    // URL of your webserver
    string weburl = "127.0.0.1";
    string uid = "?uid="   "219844";
    string secret = "secret="   "428032";
    string php;
    string output;
    string[] outputarray;
    void Start()
    {
        Read();
    }
    void Generate(){
        if (PlayerPrefs.HasKey("uid"))
        {
            uid = PlayerPrefs.GetString(uid);
            secret = PlayerPrefs.GetString(secret);
        }
        else {
            StartCoroutine(newUser("newuser", weburl, "genuser.php"));
        }       
    }
    void Read()
    {
        php = "read.php";
        StartCoroutine(readUser("read", weburl, "read.php", uid, secret));
    }
    IEnumerator newUser(string usage, string serverurl, string phpfile) {
        string ieusage = usage;
        string url = serverurl;
        string iephp = phpfile;
        if (ieusage == "newuser"){
            UnityWebRequest www = UnityWebRequest.Get(url   "/userdata/"   iephp);
            yield return www.SendWebRequest();
            output = www.downloadHandler.text;
            Debug.Log(output);
 
            if (www.result != UnityWebRequest.Result.Success) {
                Debug.Log(www.error);
            }
        }
    }
    IEnumerator readUser(string usage, string serverurl, string phpfile, string userid, string usersecret) {
        string ieusage = usage;
        string url = serverurl;
        string iephp = phpfile;
        string ieuid = userid;
        string iesecret = usersecret;
        string ieoutput = "";
        if (ieusage == "read"){
            UnityWebRequest www = UnityWebRequest.Get(url   "/userdata/"   iephp   ieuid   "&"   iesecret);
            yield return www.SendWebRequest();
            output = www.downloadHandler.text;
            ieoutput = output;
            Debug.Log(output);
 
            if (www.result != UnityWebRequest.Result.Success) {
                Debug.Log(www.error);
            }
        }
        string[] outputarray = ieoutput.Split(char.Parse("<br />"));
        for(int i = 0; i < outputarray.Length; i  )
        {
            Debug.Log(outputarray[i]);
        }
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

CodePudding user response:

char.Parse("<br />")

will give you a FormatException as char.Parse expects a string with a length of exactly 1 as the error message tells you.


You want to simply use string.Split(string)

var outPutArray = ieoutput.Split("<br />");

or for multiple separator options e.g. string.Split(string[])

var outPutArray = ieoutput.Split(new []{"<br />", "<br>", "<br/>"});

since most commonly the <br> tag is actually used without the closing /.


Since in general there are multiple ways how to exactly write these HTML tags in order to match all the different ways to write this you could also rather use Regex.Split like e.g.

var outPutArray = Regex.Split(ieoutput, "<br.*?>", RegexOptions.IgnoreCase | RegexOptions.SingleLine);

Here you match anything that

  • Begins with <br (or <BR, <Br etc)
  • Ends with the next >

not matter what comes between these, even if it is line breaks (see RegexOptions.SingleLine

See Regex Example and further Explanation on regex101.com

CodePudding user response:

Try adding an if before the Split, like below:

if(ieoutput.Contains("<br />"))
{string[] outputarray = ieoutput.Split(char.Parse("<br />"));
        for(int i = 0; i < outputarray.Length; i  )
        {
            Debug.Log(outputarray[i]);
        }
}

CodePudding user response:

If you absolutely must do it the way you are doing it. Try:

string[] outputarray = ieoutput.Split("<br />".ToCharArray())

Otherwise, since it seems you control the PHP output too, I recommend having the PHP output a JSON string. Then use Newtonsoft.Json to convert JSON string to an Object.

  • Related