Showing posts with label substring. Show all posts
Showing posts with label substring. 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

In an aspx (asp2.0) page in C# I have a string array. The array contains
letters separated into two groups by a dash (-). I want to obtain the
letters before the dash. I use the following code. Strangely, when the
array has a value of "MOD-AS" Label3 displays "3" while Label4 displays -1.
What's going on ? What is changing the value of variable mn ? Thanks,
Jim
int i = 32;
string s1;
string s2;
s1 = Ante_Arr[i];
int mn = s1.IndexOf("-");
Label3.Text = mn.ToString(); // the displayed value is 3
try
{
s2 = s1.Substring(1, mn); // throws an error: length can not be less
than zero
}
catch
{
Label4.Text = mn.ToString(); // the displayed value is -1
}Maybe it's scope?
Put the int mn = s1.IndexOf("-"); line into the try block, or take a look at
the value of mn before the s2 = s1.Substring(1, mn); line.
"Jim McGivney" <mcgiv1@.no-spam.sbcglobal.net> wrote in message
news:O7EM879dGHA.3348@.TK2MSFTNGP03.phx.gbl...
> In an aspx (asp2.0) page in C# I have a string array. The array contains
> letters separated into two groups by a dash (-). I want to obtain the
> letters before the dash. I use the following code. Strangely, when the
> array has a value of "MOD-AS" Label3 displays "3" while Label4
> displays -1.
> What's going on ? What is changing the value of variable mn ? Thanks,
> Jim
> int i = 32;
> string s1;
> string s2;
> s1 = Ante_Arr[i];
> int mn = s1.IndexOf("-");
> Label3.Text = mn.ToString(); // the displayed value is 3
> try
> {
> s2 = s1.Substring(1, mn); // throws an error: length can not be less
> than zero
> }
> catch
> {
> Label4.Text = mn.ToString(); // the displayed value is -1
> }
>

Substring

In an aspx (asp2.0) page in C# I have a string array. The array contains
letters separated into two groups by a dash (-). I want to obtain the
letters before the dash. I use the following code. Strangely, when the
array has a value of "MOD-AS" Label3 displays "3" while Label4 displays -1.
What's going on ? What is changing the value of variable mn ? Thanks,
Jim

int i = 32;
string s1;
string s2;
s1 = Ante_Arr[i];
int mn = s1.IndexOf("-");
Label3.Text = mn.ToString(); // the displayed value is 3
try
{
s2 = s1.Substring(1, mn); // throws an error: length can not be less
than zero
}
catch
{
Label4.Text = mn.ToString(); // the displayed value is -1
}Maybe it's scope?

Put the int mn = s1.IndexOf("-"); line into the try block, or take a look at
the value of mn before the s2 = s1.Substring(1, mn); line.

"Jim McGivney" <mcgiv1@.no-spam.sbcglobal.net> wrote in message
news:O7EM879dGHA.3348@.TK2MSFTNGP03.phx.gbl...
> In an aspx (asp2.0) page in C# I have a string array. The array contains
> letters separated into two groups by a dash (-). I want to obtain the
> letters before the dash. I use the following code. Strangely, when the
> array has a value of "MOD-AS" Label3 displays "3" while Label4
> displays -1.
> What's going on ? What is changing the value of variable mn ? Thanks,
> Jim
> int i = 32;
> string s1;
> string s2;
> s1 = Ante_Arr[i];
> int mn = s1.IndexOf("-");
> Label3.Text = mn.ToString(); // the displayed value is 3
> try
> {
> s2 = s1.Substring(1, mn); // throws an error: length can not be less
> than zero
> }
> catch
> {
> Label4.Text = mn.ToString(); // the displayed value is -1
> }

substring

I have a string
bobby@dotnet.itags.org.yahoo.com (Bobby Gill)

I want to parse it and want to get the substring "bobby@dotnet.itags.org.yahoo.com" before
space.
Could you please give me code.

thanks"bobby" <bobby@.discussions.microsoft.comwrote in message
news:23977997-B516-48B3-BC3C-BD5296C6B736@.microsoft.com...

Quote:

Originally Posted by

>I have a string
bobby@.yahoo.com (Bobby Gill)
>
I want to parse it and want to get the substring "bobby@.yahoo.com" before
space.
Could you please give me code.


string strRaw = "bobby@.yahoo.com (Bobby Gill)";
strEmailAddress = strRaw.Split(' ')[0];

--
Mark Rae
ASP.NET MVP
http://www.markrae.net

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
>

Substring from URL

OK, i thought I was quite experienced with asp.net, though I have a silly question. I have a photo album on my website, but at the moment most of the data is stored in a database, and I get long URLs like www.arsluminis.com/photography/album/?aid=2, I'd like to have it like www.arsluminis.com/photography/album/1/2/, so I need to get the "1" and the "2" both in a parameter, I tried it using "indexOf" and "lastIndexOf", but I can't find it.
Anyone got an idea please?

Regards,
NielsHello,
if all the urls that you have are in the form of the url that you gave :
www.arsluminis.com/photography/album/?aid=2

then, you might have something like this:

string s = url.IndexOf('=');
then you can copy the value after "=" by using any method in Strings

then, also, use the methods of String like concat and create the new url that yoy want, in the format that you want.

best of luck
Consider using the .net class System.Uri. Pass the URL into the constructor and use its Query parameter to get a string representing the querystring.

If you want to look through individual parameters, there are two ways:
1. Brute force. Use the Split('&') method on the string class to create an array of elements. Then loop through them.
2. Regular expression. I think the expression is:
Parameter name;
=
any text
? OR end of line

Here's the reference I like for building regular expressions:
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/regexp.html#1193136

The .net class is System.Text.RegularExpressions.Regex.

Substring operation

hi

I wonder of someone can help me with this Substring operation.

I have : "12.10" or "120.10"

Now I want to convert these values to : "1210" or "12010"

Basically, I want to removce the dot within my strings. How can I do this.?

Thanks

string text = "12.10";

text = text.Replace(".", "");

Substring of text...

OK, this probably is a question with a very simple answer, though I seem to be unable to find it... I'm creating my own weblog at the moment, and on the main page I would like to put a substring of the day's text ,until the first dot.

This is the code I've been trying to use :

Dim logText As String = qWebLog.myDataReader("msgText")
logText = logText.Substring(0, logText.indexOf("."))
logBodyCell.Text = logText & " <a href='?logID=" & qWebLog.myDataReader("ID") _
& "'>Read more ...</a>"

To illustrate : it should put the text in the cell till the first occurence of the dot (the end of the first sentence in other words... Though this code doesn't work, he gives the following error : length cannot be less than zero : paramatername = length.

Though, the text contains a dot ...

Who could help me please?

Regards,
NielsThe text cannot contain a dot if the error is occuring. I would suggest that you do the following:

String logText = qWebLog.myDataReader("msgText");

Int IndexOfDot = logText.IndexOf(".");

if (IndexOfDot > -1) {

logText = logText.Substring(0, IndexOfDot);
}

logBodyCell.Text = logText + " <a href='?logID=" + qWebLog.myDataReader("ID") + "'>Read more ...</a>";


Thanks a lot ! That worked ...

But now (sorry for my questioning...) I have the same problem when I type three dots... I tried it the same way :


Dim logText As String = qWebLog.myDataReader("msgText")
Dim IndexOfDot As Integer = logText.IndexOf(".")
Dim IndexOfTripleDot As Integer = logText.IndexOf("...")
If IndexOfDot > -1 Then
logText = logText.Substring(0, IndexOfDot + 1)
logBodyCell.Text = logText & " <a href='?logID=" & qWebLog.myDataReader("ID") _
& "'>Read more ...</a>"
ElseIf IndexOfTripleDot > -1 Then
logText = logText.Substring(0, IndexOfTripleDot + 1)
logBodyCell.Text = logText & " <a href='?logID=" & qWebLog.myDataReader("ID") _
& "'>Read more ...</a>"
Else
logBodyCell.Text = logText & " <a href='?logID=" & qWebLog.myDataReader("ID") _
& "'>Read more ...</a>"
End If

And it still doesn't work...

Sorry for the silly questions ...

Regards and a early happy newyear !

Regards
IndexOfDot will always be greater the -1 if IndexOfTripleDot is greater than -1, therefore the second condition will never be tested. Reverse the order of your if statements.

Also, you have six lines that do exactly the same thing. The following will achieve the same result, but with less lines of code:

Dim logText As String = qWebLog.myDataReader("msgText")

Dim IndexOfDot As Integer = logText.IndexOf(".")
Dim IndexOfTripleDot As Integer = logText.IndexOf("...")

If IndexOfTripleDot > -1 Then
logText = logText.Substring(0, IndexOfTripleDot + 1)
ElseIf IndexOfDot > -1 Then
logText = logText.Substring(0, IndexOfDot + 1)
End If

logBodyCell.Text = logText & " <a href='?logID=" & qWebLog.myDataReader("ID") _
& "'>Read more ...</a>"

substring of an array. Weird results!

I've done this many times but this is a first for this problem. I have a string which I do a split on a specific char, the $ sign.

{D}United States,Lettie,C,Letas,52 Alpha Lane,4 Schilling Lane,Test City,District of Columbia,12345-
1234,betabetty@dotnet.itags.org.here.com,101-1010,121-1212,555-555-5555,Bob Beta
{B}5/30/1976,Female,5'4,110,27,118,74,127,42,85
{L}134,bettyB01,bBob1231950,What is your favorite snack?,Gum drops
$

I then want to get specific parts of the string so I do an substring on this only to get the:

Index and length must refer to a location within the string.
Parameter name: length

error message on this bit of code. b = {B} and l = {L} which can been seen in the text above.

int t1 = separateParticipants[0].IndexOf(b);
int t2 = separateParticipants[0].IndexOf(l);
litUploadError.Text = separateParticipants[0].Substring(t1,t2);

A couple things I've tested that worked correctly.

litUploadError.Text = separateParticipants[0].Substring(0,t1);

litUploadError.Text = separateParticipants[0].Substring(0,t2);

int t1 = 0;
int t2 = separateParticipants[0].IndexOf(l);
litUploadError.Text = separateParticipants[0].Substring(t1,t2);

Since they both worked with 0, it should mean that t1 and t2 exist, so why is it that when I try to get the data between t1 and t2, it gives the error?

thanks ^_^

Your problem is that you are requesting too many characters. t2 is the index of {L}, but the number of characters that come after t2 is less than the number that come after it.

To be more clear, the definition of paramters for substring is (start Index, Number Of characters); it doesn't fetch the string between two given indices.

Your last piece of code should be:

litUploadError.Text = separateParticipants[0].Substring(t1, (t2 - t1) +1);


LOL! You are so right. Can't believe I missed that. Guess I need another beer. Thanks a bunch. :)

substring value

hi..
i just cant set strat value to have a substring value..i.e...let suppose i have string a="asd,bsd,csd,dsd";
now i want to separate asd,bsd..using substring method.
pzl help me.Split could helps you

For info seString.Split Method