C
C#7mo 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
jsOP7mo 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
Angius7mo ago
I'd probably write a parser instead tbh
js
jsOP7mo ago
i need to get the toppings back into a list
Angius
Angius7mo ago
If not that, then split by , first First element will be the name
js
jsOP7mo ago
to create my item
js
jsOP7mo ago
No description
Angius
Angius7mo ago
Then split last element by ' ' And the last part of that element will be the price
js
jsOP7mo ago
but theres multiple whitespaces before the £ sign
Angius
Angius7mo ago
Yeah, but the last segment will be the price
js
jsOP7mo ago
tomato sauce has a space and i dont want to split that
Angius
Angius7mo 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
jsOP7mo 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
Angius7mo ago
Look the method up in the documentation, see what the other parameter is
js
jsOP7mo 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
jsOP7mo ago
No description
js
jsOP7mo ago
'line 90' = string toppings = temp.Substring(nameLength, temp.IndexOf("£")); i dont get it my index values should be valid
Angius
Angius7mo ago
Use the debugger to see what they actually are
js
jsOP7mo ago
how do i do that
Angius
Angius7mo ago
$debug
MODiX
MODiX7mo 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
jsOP7mo ago
looks fine to me?
No description
js
jsOP7mo 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