6 Replies - 423 Views - Last Post: 09 July 2011 - 08:13 AM Rate Topic: -----

#1 aueddonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-July 11

string manipulation

Posted 09 July 2011 - 12:42 AM

I want to edit a string entered into a text box

There will always be 2 words and I want to take the second word and put it into a variable

i.e.

"Going Home"

would be taken as

"Home"

How do I do this?
Is This A Good Question/Topic? 0
  • +

Replies To: string manipulation

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: string manipulation

Posted 09 July 2011 - 12:54 AM

You can split string into array and get any word you want using array
string str = "Hello World";
string[] strArray = str.Split(new char[] { ' ' });
MessageBox.Show(strArray[1].ToString()); // Output: World


Was This Post Helpful? 3
  • +
  • -

#3 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: string manipulation

Posted 09 July 2011 - 01:07 AM

Hi,
Try this
            string sString = "Going Home";
            int iFirstIndex= sString.IndexOf(' ');
            sString  = sString.Substring(iFirstIndex);



Was This Post Helpful? 1
  • +
  • -

#4 aueddonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-July 11

Re: string manipulation

Posted 09 July 2011 - 01:27 AM

Thanks for the quick responses,

noorahmad I started paying with your code and it works perfectly so i'll stick with that. That SQL select is finally working!
Was This Post Helpful? 0
  • +
  • -

#5 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: string manipulation

Posted 09 July 2011 - 01:48 AM

Glad it worked :)
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,365
  • Joined: 02-June 10

Re: string manipulation

Posted 09 July 2011 - 07:59 AM

View Postnoorahmad, on 09 July 2011 - 01:54 AM, said:

You can split string into array and get any word you want using array
string str = "Hello World";
string[] strArray = str.Split(new char[] { ' ' });
MessageBox.Show(strArray[1].ToString()); // Output: World




I'm a big believer in knowing where to look things up and actually understanding WHY and HOW the code works. May I suggest you read up on the string.split method?
http://msdn.microsof...ring.split.aspx
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: string manipulation

Posted 09 July 2011 - 08:13 AM

View Postnoorahmad, on 09 July 2011 - 03:54 AM, said:

You can split string into array and get any word you want using array
string str = "Hello World";
string[] strArray = str.Split(new char[] { ' ' });
MessageBox.Show(strArray[1].ToString()); // Output: World



Just for future references, if you are only splitting on one character, you don't need to create the new character array. This will also work for splitting on one character...

string str = "Hello World";
string[] strArray = str.Split(' ');

// also, it's a string array, so all elements are strings.
// so you don't need to call .ToString on a value that's 
//    already a string.
MessageBox.Show(strArray[1]);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1