- Home /
How do you text.split a string with no delimiters/separators? (UnityScript)
Hello,
I have a string that I need to be split, so that each of the values can go into an array. Here is an example of it:
12345
67890
I tried using this code to put the values in an array.
var myTextFile : TextAsset;
function Awake(){
for (var x = 0; x< myTextFile.text.Length; x++){
if (myTextFile.text[x] != myTextFile.text[5]) {
if(myTextFile.text[x] != "\n"){
intArray.Push(myTextFile.text[x]);
}
}
}
}
When reading the values, they are not integers. I did not parse them because I don't know how to split it without any delimiters. (i.e String.Split(""[0]) I always had to change it to put commas between the numbers and use "," as a separator. Is there a way I can get around this?
Something like this(found in another answer):
var lineArray : String [] = myTextFile.text.Split("\n"[0]);
for ( var thisLine : String in lineArray ) {
var numberStrings : String [] = thisLine.Split(""[0]);
for ( var thisNumber : String in numberStrings ) {
var someInt : int = int.Parse(thisNumber);
intArray.Push(someInt);
}
}
my workaround to this was to make the textFile toString. After that I just compared the string values to see if it matched.
myStr = myTextFile.text[x].ToString();
intArray.Push(myStr);
Not exactly an intArray anymore though. (stringArray)
You are splitting a file on newlines? Here is my code(C#). There are probably more elegant solution.
char[] archDelim = new char[] { '\r', '\n' };
arstRawLines = stRawFile.Split(archDelim, StringSplitOptions.RemoveEmptyEntries);
Not exactly, I am splitting it between each number AND the lines.
In the end I would want
floatArray[0] = 1,
floatArray[1] = 2,
floatArray[7] = 6,
floatArray[9] = 0,
something like that.
Answer by whydoidoit · Feb 04, 2013 at 10:45 PM
Try something like this?
import System.Linq;
...
var intArray = myTextFile.text.Where(function(c) c != "\n"[0]).Select( function(c) int.Parse(c.ToString())).ToArray();
Not sure what that thing was about checking position 5 in the text array -> if you needed that then:
var intArray = myTextFile.text.Where(function(c) c != '\n' && c != myTextFile.text[5]).Select( function(c) int.Parse(c)).ToArray();
I tried using your code, and it says "no appropriate version of int.Parse for the argument list '(char)' was found."
I think I ran into this problem before, in that the text file I'm using is reading chars and not ints. That's why I had to do something like that second snippet of code I attached to make it into a string, and then parse the string. Correct me if I'm wrong.
The thing about checking position 5 was because I didn't know what was there, I was sure it was the newline "\n" but when checking for that, the statement wouldn't go through.
Apologies if I am wording this awkwardly, this is my first time on the forums.
@Polinator: use int.Parse(c.ToString())
.
@$$anonymous$$ike: '\n'
should be "\n"[0]
in Unityscript.
You could try:
var intArray = myTextFile.text.Where(function(c) char.IsDigit(c)).Select( function(c) int.Parse(c.ToString())).ToArray();
Answer by hoy_smallfry · Feb 04, 2013 at 11:35 PM
You're trying to put each number digit in an array? "12345" -> {"1", "2", "3", "4", "5"} Correct?
To do that:
var str : String = "12345";
var strArray : String [] = str.Split("");
var intArray : int [] = strArray.map(function(x){return parseInt(x)});
Hope that helps.
Yes but I'm reading it from a text file, and str.Split(""); wouldn't work. Also I think it's
var strArray : String [] = str.Split(""[0]);
for UnityScript, like Eric stated below in another answer. (With the [0])
Answer by zenedee · Jul 07, 2014 at 09:04 AM
Find here more about c# string split ....String Split()
Zenee
Wouldn't really help in this case given there are no delimiters
Your answer
![](https://koobas.hobune.stream/wayback/20220613092141im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Questions about render distance in an open world game 0 Answers
parseInt a String not working 2 Answers
Cutting simple plane with a line (~2D) 2 Answers
split a scene into foreground and background in vr game. 0 Answers
Is it possible to round down 0.99 to 0? 2 Answers