C
C#8mo ago
js

manipulating string

foreach (var pizza2 in pizzas)
{
if (selection <= FoodMenu.pizzas.Count)
{
string temp = pizza2.ToString();
string name = temp.Substring(0, temp.IndexOf(","));
string toppings = temp.Substring(name.Length, );
int price = int.Parse((""));
Pizza pizza = new Pizza(name, toppings, price);
}
}
foreach (var pizza2 in pizzas)
{
if (selection <= FoodMenu.pizzas.Count)
{
string temp = pizza2.ToString();
string name = temp.Substring(0, temp.IndexOf(","));
string toppings = temp.Substring(name.Length, );
int price = int.Parse((""));
Pizza pizza = new Pizza(name, toppings, price);
}
}
No description
22 Replies
js
jsOP8mo ago
i need to separate name from toppings and price to then create an instance of an item ive managed to separate the name but how would I make a substring from the first comma to the £ sign
Angius
Angius8mo ago
I'd probably write a parser instead tbh
js
jsOP8mo ago
i need to get the toppings back into a list
Angius
Angius8mo ago
If not that, then split by , first First element will be the name
js
jsOP8mo ago
to create my item
js
jsOP8mo ago
No description
Angius
Angius8mo ago
Then split last element by ' ' And the last part of that element will be the price
js
jsOP8mo ago
but theres multiple whitespaces before the £ sign
Angius
Angius8mo ago
Yeah, but the last segment will be the price
js
jsOP8mo ago
tomato sauce has a space and i dont want to split that
Angius
Angius8mo ago
You just split it to extract the price
var nameAndRest = str.Split(',');
var name = nameAndRest[0];
var rest = nameAndRest[1..];
var priceStr = rest[^1].Split(' ')[^1];
var nameAndRest = str.Split(',');
var name = nameAndRest[0];
var rest = nameAndRest[1..];
var priceStr = rest[^1].Split(' ')[^1];
js
jsOP8mo ago
intPriceTemp = intPriceTemp.Remove(intPriceTemp.IndexOf("£")); intPriceTemp = intPriceTemp.Remove(intPriceTemp.IndexOf(".")); im getting an error am i supposed to have 2 arguments inside .remove i only want to remove that specific character
Angius
Angius8mo ago
Look the method up in the documentation, see what the other parameter is
js
jsOP8mo ago
i know now but its still saying my index numbers are invalid
foreach (var pizza2 in pizzas2)
{
if (selection <= FoodMenu.pizzas.Count)
{
string temp = pizza2.ToString();
string name = temp.Substring(0, temp.IndexOf(","));
int nameLength = name.Length;
string toppings = temp.Substring(nameLength, temp.IndexOf("£"));
foreach (var pizza2 in pizzas2)
{
if (selection <= FoodMenu.pizzas.Count)
{
string temp = pizza2.ToString();
string name = temp.Substring(0, temp.IndexOf(","));
int nameLength = name.Length;
string toppings = temp.Substring(nameLength, temp.IndexOf("£"));
js
jsOP8mo ago
No description
js
jsOP8mo ago
'line 90' = string toppings = temp.Substring(nameLength, temp.IndexOf("£")); i dont get it my index values should be valid
Angius
Angius8mo ago
Use the debugger to see what they actually are
js
jsOP8mo ago
how do i do that
Angius
Angius8mo ago
$debug
MODiX
MODiX8mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
js
jsOP8mo ago
looks fine to me?
No description
js
jsOP8mo ago
starting index is nameLength 10 then end index is £ sign which is near end of temp anyone got an idea
Want results from more Discord servers?
Add your server