- Home /
.js to C# conversion
Hi,
I'm using the below function to get the number from the game objects name or tag in .js
What I need to do is add the same function to a C# script but I really don't know how to write this in C#
Can someone help me out please .. Cheers.
// -----------------------------------------------------------------
// Function to extract the number from the GameObjectX.tag or .name
// -----------------------------------------------------------------
function ConvertToInt(stringContainingNumber : String) : int{
return System.Convert.ToInt32(stringContainingNumber.Substring(stringContainingNumber.ToList().FindIndex(function(c) char.IsDigit(c))));
}
// -----------------------------------------------------------------
Answer by Gruffy · Mar 20, 2014 at 03:54 PM
Hey Griffo, I have edited my answer AGAIN. this should give you the result you wanted. take care for now dude.
This is now arbitrary and requires no special set up. just add this to a gameobject or camera, then add the gameobject you want to it. form there , you can call your gameobject whatever you like, the regex will search until the first number appears.
I have coded it to show up in the debug.log, of which you can take that value held in result and do what you like with it as an integer.
returning it(i.e. renaming the go.gameObject.nameis another thing but probably just a reverse of this in some way) take care and thought this was a great question. Gruffy
Newly Edited code below
using UnityEngine;
using System;
using System.Collections;
using System.Text.RegularExpressions;
public class UsingRegExp : MonoBehaviour
{
public GameObject go;
private string cachedStringForParsing = null;
private string resultingValueFound = null;
private string regexPattern = @"\d";
private int result = 0;
private Regex regex;
private Match match;
// Use this for initialization
void Start ()
{
cachedStringForParsing = go.gameObject.name;
Debug.Log(cachedStringForParsing);
}
private void ConvertStringtoInt(string value)
{
try
{
result = Int32.Parse(value);
// Debug.Log(result.ToString());
}
catch (FormatException e)
{
Debug.LogException(e);
return;
}
}
// Update is called once per frame
void Update ()
{
//resultingValueFound = cachedStringForParsing;
resultingValueFound = Regex.Match(cachedStringForParsing, regexPattern).Value;
ConvertStringtoInt(resultingValueFound);
Debug.Log("This " + result + " is now cached in integer an form");
}
}
Hi,
I get an error .. The name `ConvertToInt' does not exist in the current context
with the above code.
you must write on the top using System; or replace ConvertToInt by System.Convert.ToInt32
O$$anonymous$$, I'd already got using System.Linq; at the top so I replaced ConvertToInt to System.Convert.ToInt32, and that got rid of the error, but when I call the function with ConvertStringToInt(transform.tag); I get this error ..
FormatException: Input string was not in the correct format
On the line .. int converted = System.Convert.ToInt32(str);
I don't think that for example "Untagged" can be converted to numbers.
So I have to eat, so Im told.. So here is my code so far.
i think it`ll give a format exception error, so will need some more looking at . I will be bakc in a mo, but if you fancied looking about / or over this code, you should get the gist as we are only utilizing the RegEx class of .NET.
public class UsingRegExp : $$anonymous$$onoBehaviour
{
public GameObject go;
private String cachedStringForParsing = null;
private String resultingValueFound = null;
private int result = 0;
// Use this for initialization
void Start ()
{
cachedStringForParsing = go.gameObject.name;
Debug.Log(cachedStringForParsing);
}
// Update is called once per frame
void Update ()
{
resultingValueFound = cachedStringForParsing;
resultingValueFound = Regex.$$anonymous$$atch(cachedStringForParsing, @"/d+").Value;
Debug.Log(resultingValueFound);
Int32.Parse(resultingValueFound);
Debug.Log("This" + resultingValueFound + " is now cached in integer form");
}
}
Answer by adsamcik · Mar 20, 2014 at 03:20 PM
This might help http://www.m2h.nl/files/js_to_c.php
You just have to rewrite data type if there is var in js, because js doesn't specify data types.
O$$anonymous$$, using the converter I get ..
int ConvertToInt ( string stringContainingNumber ){ return System.Convert.ToInt32(stringContainingNumber.Substring(stringContainingNumber.ToList().FindIndex(function(c) char.IsDigit©))); }
And get the error .. error CS1525: Unexpected symbol `char'
Answer by Griffo · Mar 20, 2014 at 07:51 PM
Think I've found the answer ..
string numbersOnly = Regex.Replace(transform.tag, "[^0-9]", "");
Adding ..
using System.Text.RegularExpressions;
At the top, seem to work .. Then turn numbersOnly into a int.
int tagNumber = Convert.ToInt32(numbersOnly);
yup, sounds similar to where the code i recently posted was going You may be better off with Parse() ins$$anonymous$$d of Convert.ToInt(). Im just looking at it now
Hey Griffo, I did it, and wrapped it up nice for you in my edited answer. the whole issue i had all along (format exception error) was due to my stupidity where is should have types @"\d+" i did in fact type @"/d+" causing a mahoosive issue for obvious reasons based around parsing structure of regex itself(forward and back slashes having signifcantly different meanings etc) anyway, like I said, solved for you in my edited Answer above. Take care Gruffy
Answer by Penzin · Apr 05, 2014 at 07:08 AM
This function takes a string and constructs a list containing all ints found in the string.
public List<int> ConvertToInt(string Input)
{
List<int> Results = new List<int>(Input.Length);
for (int i = 0; i < Input.Length; i++)
{
if (char.IsDigit(Input[i]))
{
Results.Add(Convert.ToInt32(char.GetNumericValue(Input[i])));
}
}
Results.TrimExcess();
return Results;
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Create a guitexture When clicked on 3d text? 0 Answers
js to C# converstion Problem 3 Answers
Convert js to C# Serializer problem 1 Answer
Converting from js to c# 1 Answer