- Home /
Check for characters specifically in string not everytime it appears
Im trying to input numbers as a string and print them out if a certain number was found.
Lets say the number was "1". How would I check if the string contained "1" and not every "1" that appears such as "10" "14" etc..
Ive tried variableString.contains("stringneeded") but it doesnt check for specifics.
Im using C# btw :)
Answer by kdubnz · Dec 03, 2014 at 11:09 AM
Will the string only contain a number ?
string sInput = "12";
int number;
bool result = Int32.TryParse(sInput, out number);
if (result)
Console.WriteLine("number: {0}", number);
//----------------------
Do you only want the first number if there are more than one ?
string inputData = "a11is2for3apple,1shiny231and1.1red";
var data = Regex.Match(inputData, @"\d+").Value;
Console.WriteLine("string data: {0}", data);
//----------------------
Do you want a list of ALL numbers ?
string input = "a11is2for3apple,1shiny231and1.1red";
string[] numbers = Regex.Split(input, @"\D+");
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
int i = int.Parse(value);
Console.WriteLine("Integer: {0}", i);
}
}
//----------------------
Result:
number: 12
string data: 11
Integer: 11 Integer: 2 Integer: 3 Integer: 1 Integer: 231 Integer: 1 Integer: 1
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I use just one script to move randomly several characters from an array? 0 Answers
Renderer on object disabled after level reload 1 Answer
Inventory Script. List Contains. I dont know the need bit of code.[UNSOLVED] 2 Answers