Reading a specific word/phrase from a line
-
Hi Not sure if this was already asked, but I'm trying to find a way to get a certain text (or phrase) from a line in notepad using C# example: (notepad) >Name: Joseph Smith >Address: Main Street I need to get name (Joseph Smith) and the address only, ignoring the "name:" and "address:" so far i only know how to read the entire line, but i have no idea how in the world i can isolate the desired text/phrase Hope you guys can help. Thanks
-
Hi Not sure if this was already asked, but I'm trying to find a way to get a certain text (or phrase) from a line in notepad using C# example: (notepad) >Name: Joseph Smith >Address: Main Street I need to get name (Joseph Smith) and the address only, ignoring the "name:" and "address:" so far i only know how to read the entire line, but i have no idea how in the world i can isolate the desired text/phrase Hope you guys can help. Thanks
If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here.
------------ Cheers, Patrick
-
If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here.
------------ Cheers, Patrick
Why don't you try a xml file?
:^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
-
Why don't you try a xml file?
:^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
He specifically said he's getting a line from a text file in notepad and gave an example line. XML works nicely, yes, but unfortunately not everyone uses XML to store application information ;)
------------ Cheers, Patrick
-
If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here.
------------ Cheers, Patrick
Patrick Sears wrote:
If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here. ------------ Cheers, Patrick
Hi Patrick! thanks for the help. Yes, im using C#, but i received this error "Error 1 'string' does not contain a definition for 'split'" for string[] parts = string**.split**(line, ":"); do i need to import something? thanks again
-
Patrick Sears wrote:
If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here. ------------ Cheers, Patrick
Hi Patrick! thanks for the help. Yes, im using C#, but i received this error "Error 1 'string' does not contain a definition for 'split'" for string[] parts = string**.split**(line, ":"); do i need to import something? thanks again
Split isn't a static method.Use in in a object instance and take care of the case.
:^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
-
Patrick Sears wrote:
If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here. ------------ Cheers, Patrick
Hi Patrick! thanks for the help. Yes, im using C#, but i received this error "Error 1 'string' does not contain a definition for 'split'" for string[] parts = string**.split**(line, ":"); do i need to import something? thanks again
Sorry, xibeifeijian has it right - it's an instance method, I was just indicating what class it belongs to. Sorry about that :( Sometimes I'm not sure if people will understand I'm speaking in pseudocode; sort of depends on experience level which I don't know up front. Let me know if you need anymore help figuring out how to do the parsing!
------------ Cheers, Patrick