public class Libro
{
public string Titolo { get; set; }
public string Autore { get; set; }
public string Editore { get; set; }
public int ISBN { get; set; }
public int Pagine { get; set; }
public decimal Prezzo { get; set; }
public int Quantità { get; set; }
not being all of type string, I would not know how to convert the int and decimal values to have as a result a table in which to display a list of books (titles, authors, publishers and price) that I have on a file.txt
public Libro BuildLibro(string input)
{
Libro result = null;
if (!String.IsNullOrEmpty(input))
{
var inputArray = input.Split('*');
if (inputArray.Length >= 6)
{
result = new Libro();
result.Titolo = inputArray[0];
result.Autore = inputArray[1];
result.Editore = inputArray[2];
if (!string.IsNullOrEmpty(inputArray[3]))
{
int.TryParse(inputArray[3], out int num);
result.ISBN= num;
}
if (!string.IsNullOrEmpty(inputArray[4]))
{
int.TryParse(inputArray[4], out int num);
result.Pagine = num;
}
if (!string.IsNullOrEmpty(inputArray[5]))
{
decimal.TryParse(inputArray[5], out decimal num);
result.Prezzo = num/100;
}
if (!string.IsNullOrEmpty(inputArray[6]))
{
int.TryParse(inputArray[6], out int num);
result.Quantità = num;
}
}
}
return result;
}
}
}
CodePudding user response:
Foreword
The question has been edited massively since this answer was posted. I've advised that the edits should be reverted so this answer remains valid for the question as posted prior to the edit on Feb 14th (Revision 3), and a new question posted containing Rev 3's text
Original advice for Revision 2
You've switched tactic when you got to ISBN
ISBN = 4, Pagine = 5, Prezzo = 6, Quantità = 7
Assigning the editore name like this makes sense:
Editore = content[3]
It means "take the fourth element of the content
array, which is a string array, and put it in the editors property, which is a string"
I guess you'll have tried the pattern of:
ISBN = content[4]
But this won't have worked out because content
is full of strings and ISBN is an int
and even if a string purely only full of numerical chars that doesn't mean it's a number. This will have given some error like "there is no implicit conversion..."
I guess then you removed the content and just left a hard coded 4 assigned as the ISBN which will work in that it will compile syntactically, but it is incorrect logically. That will just fix every ISBN at being 4
, literally
Instead you should parse the string to an int. it's easy, and we can do it with like:
ISBN = int.Parse(content[4])
Similarly for the decimal there is a decimal.Parse
This may expose other problems like, if one of the values contains some non numerical chars (e.g. ISBNs that have hyphens in) but we can solve that later...
CodePudding user response:
There are many ways to parse information out of a string.
I would sugest using regex. For example with the following regex you could parse three strings separated by a *:
(?'Title'[^*] )[*](?'Author'[^*] )[*](?'ISBN'[^*] )
The Advantage of using regex is, that you check if the string is valid while the string is parsed. To test a regex I would sugest to use something like https://regex101.com
In your code you can use the C# library:
using System.Text.RegularExpressions;
Regex rx = new Regex(@"(?'Title'[^*] )[*](?'Author'[^*] )[*](?'ISBN'[^*] )",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string.
string text = "Harry Potter*Rowling*12345";
// Find matches.
MatchCollection matches = rx.Matches(text);