28 Replies - 1159 Views - Last Post: 08 December 2009 - 10:03 AM
#1
"String" manipulation
Posted 07 December 2009 - 10:07 AM
I have a problem. I am creating a program, and that shall get a name and lastname, in example a teksteditor.
It's writted like this.
Olsen Peter
and i want to create a c# program, there will do so when it Start at O and find space, it take "Olsen" and copy and take Peter and put that at the first one, so it like this.
Peter Olsen
And then shall Peter Olsen be pasted in Excel if that is possible, Ty
Hope you can help me..
Replies To: "String" manipulation
#2
Re: "String" manipulation
Posted 07 December 2009 - 10:22 AM
http://msdn.microsof...ring.split.aspx
for example, if I wanted to split the following string on the comma, I would to this...
string test = "some,where,over,the,rainbow";
string[] array = test.Split(new char[]{ ',' })'
StringBuilder sb = new StringBuilder();
foreach(string s in array)
{
sb.Append(s + " ");
}
MessageBox.Show(sb.ToString());
This post has been edited by eclipsed4utoo: 07 December 2009 - 10:24 AM
#3
Re: "String" manipulation
Posted 07 December 2009 - 10:22 AM
string inputText = "Olsen Peter";
string[] splittedString = inputText.Split(' ');
string lastName = splittedString[0];
string name = splittedString[1];
But this only works if a person doesn't have a middle name or even a two words last name, because it's impossible to tell which part of the string is a name and which represents lastname.
Better solution would be to have two have separate textboxes for name and lastname, if that is possible in your application.
This post has been edited by FlashM: 07 December 2009 - 10:24 AM
#4
Re: "String" manipulation
Posted 07 December 2009 - 11:11 AM
#5
Re: "String" manipulation
Posted 07 December 2009 - 11:12 AM
Well my application shall get about 100 names at one time, to sort out.
And the names are writted like Olsen Peter and shall be to Peter Olsen..
How would u make it to change, when there are so many names ? :-)
#6
Re: "String" manipulation
Posted 07 December 2009 - 11:15 AM
ahlmo, on 7 Dec, 2009 - 10:12 AM, said:
Well my application shall get about 100 names at one time, to sort out.
And the names are writted like Olsen Peter and shall be to Peter Olsen..
How would u make it to change, when there are so many names ? :-)
I don't understand your question
Did you mean how to swap two words?
#7
Re: "String" manipulation
Posted 07 December 2009 - 11:31 AM
FlashM, on 7 Dec, 2009 - 10:15 AM, said:
ahlmo, on 7 Dec, 2009 - 10:12 AM, said:
Well my application shall get about 100 names at one time, to sort out.
And the names are writted like Olsen Peter and shall be to Peter Olsen..
How would u make it to change, when there are so many names ? :-)
I don't understand your question
Did you mean how to swap two words?
Yea abit, but it will be like this:
Olsen Peter
Andreasen Anders
Mikkelsen Poul
And so on ---->
It just need to do so the first name, also Peter and so on will be the first one.
and Olsen and so on will be the latest.. like this
Peter Olsen
Anders Andreasen
Poul Mikkelsen
and so on..
Hope u understand me now
#8
Re: "String" manipulation
Posted 07 December 2009 - 11:58 AM
string newString = string.Format("{0} {1}", name, lastname);
This post has been edited by FlashM: 07 December 2009 - 11:58 AM
#9
Re: "String" manipulation
Posted 07 December 2009 - 12:27 PM
But how do you do that with 100 ?
Ind a richbox ex
#10
Re: "String" manipulation
Posted 07 December 2009 - 12:59 PM
int i;
foreach (int i in SomeArray)
{
//Do whatever needs to be done to each string here
}
Though you'd only have to split it once then go through that afterwards. Look up arrays, should help.
#11
Re: "String" manipulation
Posted 07 December 2009 - 01:02 PM
ahlmo, on 7 Dec, 2009 - 03:27 PM, said:
But how do you do that with 100 ?
Ind a richbox ex
ok, let me try to sum this up and see if I have it correct.
You have a RichTextBox that will contain 100 lines. Each line will have a "last name first name". You want to take all of those names and switch them to "first name last name"?
#12
Re: "String" manipulation
Posted 07 December 2009 - 01:07 PM
eclipsed4utoo, on 7 Dec, 2009 - 12:02 PM, said:
ahlmo, on 7 Dec, 2009 - 03:27 PM, said:
But how do you do that with 100 ?
Ind a richbox ex
ok, let me try to sum this up and see if I have it correct.
You have a RichTextBox that will contain 100 lines. Each line will have a "last name first name". You want to take all of those names and switch them to "first name last name"?
Correct, but it shall take more than 100 if possible
#13
Re: "String" manipulation
Posted 07 December 2009 - 01:12 PM
foreach (string line in rtbTextbox.Lines)
{
Console.WriteLine(line);
}
#14
Re: "String" manipulation
Posted 07 December 2009 - 01:22 PM
#15
Re: "String" manipulation
Posted 07 December 2009 - 01:29 PM
Foreach statement is very similar to For Loop.
|
|

New Topic/Question
Reply




MultiQuote






|