"String" manipulation

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 1478 Views - Last Post: 08 December 2009 - 10:03 AM Rate Topic: -----

#1 ahlmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-November 09

"String" manipulation

Posted 07 December 2009 - 10:07 AM

Hello.

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..

Is This A Good Question/Topic? 0
  • +

Replies To: "String" manipulation

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: "String" manipulation

Posted 07 December 2009 - 10:22 AM

You will want to look into the String.Split function.

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

Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: "String" manipulation

Posted 07 December 2009 - 10:22 AM

One thing you could do is to split the string, using space as the splitter like so:

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

Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: "String" manipulation

Posted 07 December 2009 - 11:11 AM

There is also the IndexOf() you can check it in MSDN.
Was This Post Helpful? 0
  • +
  • -

#5 ahlmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-November 09

Re: "String" manipulation

Posted 07 December 2009 - 11:12 AM

Thanks for answers guys.

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 ? :-)
Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: "String" manipulation

Posted 07 December 2009 - 11:15 AM

View Postahlmo, on 7 Dec, 2009 - 10:12 AM, said:

Thanks for answers guys.

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 :blink:

Did you mean how to swap two words?
Was This Post Helpful? 0
  • +
  • -

#7 ahlmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-November 09

Re: "String" manipulation

Posted 07 December 2009 - 11:31 AM

View PostFlashM, on 7 Dec, 2009 - 10:15 AM, said:

View Postahlmo, on 7 Dec, 2009 - 10:12 AM, said:

Thanks for answers guys.

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 :blink:

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
Was This Post Helpful? 0
  • +
  • -

#8 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: "String" manipulation

Posted 07 December 2009 - 11:58 AM

Use the split method I wrote and then concat two strings using String.Format method:

string newString = string.Format("{0} {1}", name, lastname);

This post has been edited by FlashM: 07 December 2009 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#9 ahlmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-November 09

Re: "String" manipulation

Posted 07 December 2009 - 12:27 PM

Ty.

But how do you do that with 100 ?

Ind a richbox ex
Was This Post Helpful? 0
  • +
  • -

#10 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,514
  • Joined: 28-November 09

Re: "String" manipulation

Posted 07 December 2009 - 12:59 PM

Put all of the items in an array and loop through it.

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.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: "String" manipulation

Posted 07 December 2009 - 01:02 PM

View Postahlmo, on 7 Dec, 2009 - 03:27 PM, said:

Ty.

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"?
Was This Post Helpful? 0
  • +
  • -

#12 ahlmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-November 09

Re: "String" manipulation

Posted 07 December 2009 - 01:07 PM

View Posteclipsed4utoo, on 7 Dec, 2009 - 12:02 PM, said:

View Postahlmo, on 7 Dec, 2009 - 03:27 PM, said:

Ty.

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 :)
Was This Post Helpful? 0
  • +
  • -

#13 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: "String" manipulation

Posted 07 December 2009 - 01:12 PM

Try:

foreach (string line in rtbTextbox.Lines)
{
	Console.WriteLine(line);
}


Was This Post Helpful? 0
  • +
  • -

#14 ahlmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-November 09

Re: "String" manipulation

Posted 07 December 2009 - 01:22 PM

Hmm Can you describe what it's do :)?
Was This Post Helpful? 0
  • +
  • -

#15 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: "String" manipulation

Posted 07 December 2009 - 01:29 PM

It iterates through all the lines in your RichTextbox control and does something with each line (in my case outputs into Console window).

Foreach statement is very similar to For Loop.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2