x


How to only return numbers with StreamReader?

I am not very familiar with writing and reader files yet, so I'm having a tough time figuring this out.

Using StreamWriter, I'm writing the following to a .text file:

UpgradePointsTotalAmount = 5250

Now I need to retrieve the number only, by first checking if "UpgradePointsTotalAmount" exists, and then returning its value.

How would I do that using StreamReader?

Thanks in advance for your time! Stephane

more ▼

asked Sep 03 '11 at 07:21 PM

ronronmx gravatar image

ronronmx
806 84 100 121

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Try this out:

using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;

public class regex : MonoBehaviour 
{
    public string s = "afsdlk 10 sdfalkj 30";

    // Update is called once per frame
    void Start()
    {
       string[] numbers = Regex.Split(s, @"\D+");
       foreach (string value in numbers)
       {
         if (!string.IsNullOrEmpty(value))
         {
         int i = int.Parse(value);
         Debug.Log("Number: " + i);
         }
       }
    }
}

I wrote this code by adapting the example from this site: http://www.dotnetperls.com/get-numbers-from-string

more ▼

answered Sep 03 '11 at 08:44 PM

karl_ gravatar image

karl_
2.4k 41 53 70

Awesome! This works exactly how I needed it to work, thanks a lot for your help kari :)

Sep 03 '11 at 09:17 PM ronronmx
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x17

asked: Sep 03 '11 at 07:21 PM

Seen: 526 times

Last Updated: Sep 03 '11 at 09:17 PM