1
votes

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()
    {
        
    }
}
3
char.Parse("<br />") <-- This does not do what you think it does. - Dai
I'd be lying if I said I didn't grab it from an example of splitting to an array. - Brendan Ocheltree
Well, whatever code it was, I can assure you that code is completely incorrect - and I disagree with your self-assessment that you are "pretty fluent" considering that the documentation for Char.Parse makes it very clear that the argument value must have exactly 1 character in it - and the exception message is self-explanatory. This suggests that you do not yet understand what a character-value is, which is a very fundamental unit in programming, therefore you cannot be "pretty fluent". - Dai
Are you just trying to split by "<br />"? - Llama
In that case, I would strongly suggest using something like JSON to exchange data between PHP and C#. - Llama

3 Answers

1
votes
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


However as was already suggested, if you can change the output format actually the simplest solution of all would be rather use line breaks (\n). It is a single character so you can simply use

var outPutArray = ieoutput.Split('\n');
-5
votes

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]);
        }
}
-5
votes

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.