- Home /
what package?namespace? do I need to make use of String.Format() for C# programs?
I've found a bunch of C# reference materials on how to format strings using String.Format()
e.g.
timeText = String.Format ("{0:00}:{0:00}:{1:00}:{0:00}",displayDays,displayHours, displayMinutes, displaySeconds);
But ... when I write a C# script in Unity I attempting to make use of String.Format() I get the error message:
The name "String" does not exist in the current content
So I assume I need to add a "using .." statement at the top of of the script file .. but which one?
Using String; doesn't work
any help much appreciated ...
Answer by Eric5h5 · Feb 06, 2011 at 11:19 PM
"String" = Javascript, "string" = C#.
"string" in C# is just shorthand for "System.String" (just as "int" is shorthand for "System.Int32", etc.)
Indeed, but that doesn't change anything, and isn't particularly relevant here.
OP was asking what namespace to use, answer is System. Understanding aliases for basic types is relevant to understanding that every single type in the language is an object type in a namespace.
Yes, but you don't need it; you can just use string.Format. (Or String.Format in JS.)
Answer by matt smitih · Feb 06, 2011 at 11:12 PM
okay - slightly odd - I found a different article that used "string.Format()" (i.e. lower case "s")
and this solves the problem :-)
since I assume (perhaps wrongly) that I'm not the only person who might find this odd, I'll leave my question and answer posted, so it might help someone else save 20 minutes trying to get String.Format() rather than string.Format() working in C# scripts
happy coding ... matt ..
Answer by yoyo · Feb 06, 2011 at 11:32 PM
You need ...
using System;
This can cause namespace conflicts. For example Random.Range; Unity doesn't know if that's supposed to be System.Random or UnityEngine.Random, so then you have to explicitly specify, which is a bit of a pain.
I've got "using System;" at the top of dozens and dozens of scripts and in case of conflict I use "using Random = UnityEngine.Random;". I don't have many conflicts, but I guess it depends how random your game is ;)
Answer by matt smitih · Mar 09, 2011 at 08:35 PM
here's a more explicit version of my final working code (simplified for exposition):
void OnGUI() { float displayDays = 13; float displayHours = 9; float displayMinutes = 32; float displaySeconds = 55; string timeText = System.String.Format ("{0:00}:{0:00}:{1:00}:{0:00}",displayDays,displayHours, displayMinutes, displaySeconds); GUILayout.Label("time = " + timeText );
}
This has been very illuminating - it is very useful to learn about these "aliases" of classes to act as primitive types (personally I've never been happy with primitive types in any OO language, they spoil the clarity and simplicity of taking an objects-only view - I've always had a softspot for Smalltalk ...)
I found the following link useful, which summarises a list of aliases and the actual classes they are aliases for:
http://en.csharp-online.net/CSharp_FAQ:_What_is_a_type_alias
while UnityAnswers keeps asking me if I want to answer my own question, I keep saying yes ... since I think this discussion is something useful for people like me who can program (I've been programming for over 30 years - they keep on creating new programming concepts like design patterns and object-orientation, and lots of new languages like Python and ActionScript) but are new to C# and unity ...
now to get back to that pacman tile-based world game sample I'm getting ready for my lecture on Friday to some year 2 BSc computing students...
.. matt ..
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
String format to show float as time 3 Answers
Multiple Cars not working 1 Answer
System.DateTime truncate Milliseconds 1 Answer
How to convert timer to minutes, seconds and cents 3 Answers