.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))));
}
// -----------------------------------------------------------------

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");
	}
}

This might help Convert unity javascript (unityscript) to C#

You just have to rewrite data type if there is var in js, because js doesn’t specify data types.

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);

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*))*
  •  {*
    

_ Results.Add(Convert.ToInt32(char.GetNumericValue(Input*)));_
_
}_
_
}*_

* Results.TrimExcess();*

* return Results;*
}