Showing posts with label john. Show all posts
Showing posts with label john. Show all posts

Wednesday, March 28, 2012

Substring

Hello

I have the following string:

myName = "John Smith Curtis"

I want to get the first word, i.e. "John", which means I want to get everything from the start to the first space.

I know I should use Substring but I don't know what to do to remove the first word.

Thanks,
Miguelstring myName = "John Smith Curtis";
myName = myName.Substring( 0, myName.IndexOf( " " ) );

The best way to go though would be to do

myName.Split(" ")(0) -> That will return the first element before the space

If you really want to us substring then do

myName.Substring(0,4)


myName.Remove(myName.IndexOf(' ')) will return "John"


try the following ( i didnt check for going out of bound to keep the example simple)

string myName = "John Smith Curtis";

//get the index of the frist space
int index = myName.IndexOf(" ");

//get the first word
string firstname = myName.Substring(0, index);

//remove the first word
myName = myName.Remove(0, index + 1);

Substring and ()

Hello,

I have a string which looks as follows: "asdksdjfssdf@dotnet.itags.org.sdfsdfsdfsdfsdf.com (John Smith)"

How to create a substring with only the part which is between (), i.e., the name?

Thanks,

Miguel

you could either use thestring.indexof to find the locations of the parenthesis and then extract the substring..

or you could look at using regex:http://aspnet.4guysfromrolla.com/articles/022603-1.aspx


Consider

PublicFunction GetPersonsName(ByVal ItemTextAsString)AsString

' Sample data - expects constant format as provided, and without

' sure its not really possible anyway.

' "asdksdjfssdf@.sdfsdfsdfsdfsdf.com (John Smith)"

Dim SplitChars()AsChar = {"(",")"}

Return ItemText.Split(SplitChars)(1)' Returns John Smith

EndFunction

rgds,

Martin.

Substring and ()

Hello,
I have a string which looks as follows:
"asdksdjfssdf@dotnet.itags.org.sdfsdfsdfsdfsdf.com (John Smith)"
How to create a substring with only the part which is between (), i.e.,
the name?
Thanks,
MiguelThere are a couple of ways:
1. Use a Regular Expression
2. Split the string on '(' and remove the ')' from the resulting string
3. Find the position of '(' and ')' with Regex or string search and
substring using these two values to get start and end.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
****************************************
*********
Think outside of the box!
****************************************
*********
"shapper" <mdmoura@.gmail.com> wrote in message
news:1159644569.490605.218750@.k70g2000cwa.googlegroups.com...
> Hello,
> I have a string which looks as follows:
> "asdksdjfssdf@.sdfsdfsdfsdfsdf.com (John Smith)"
> How to create a substring with only the part which is between (), i.e.,
> the name?
> Thanks,
> Miguel
>

Substring and ()

Hello,

I have a string which looks as follows:
"asdksdjfssdf@dotnet.itags.org.sdfsdfsdfsdfsdf.com (John Smith)"

How to create a substring with only the part which is between (), i.e.,
the name?

Thanks,

MiguelThere are a couple of ways:

1. Use a Regular Expression
2. Split the string on '(' and remove the ')' from the resulting string
3. Find the position of '(' and ')' with Regex or string search and
substring using these two values to get start and end.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
*************************************************
Think outside of the box!
*************************************************
"shapper" <mdmoura@.gmail.comwrote in message
news:1159644569.490605.218750@.k70g2000cwa.googlegr oups.com...

Quote:

Originally Posted by

Hello,
>
I have a string which looks as follows:
"asdksdjfssdf@.sdfsdfsdfsdfsdf.com (John Smith)"
>
How to create a substring with only the part which is between (), i.e.,
the name?
>
Thanks,
>
Miguel
>