Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / September 2005

Tip: Looking for answers? Try searching our database.

string question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Howard - 06 Sep 2005 17:51 GMT
Hello, I'm new to c$
I need help writing a function that matches a word only if the word is
at the end of a string
example:
string a = "I like google";
string b = "google I like";
string a would return true, b would return false

secondly i need to write a function that checks if the length of my
strings is less or equal to 20 characters. if more than 20 chars then
remove the extra ones.

Thanks,

Howard
Mythran - 06 Sep 2005 17:58 GMT
> Hello, I'm new to c$
> I need help writing a function that matches a word only if the word is
[quoted text clipped - 11 lines]
>
> Howard

Public Function AtEndOfString(ByVal Text As String, ByVal Word As String) As
Boolean
   Return Text.EndsWith(Word)
End Function

Public Function GetBeginningOfString(ByVal Text As String, ByVal Count As
Integer) As String
   Return Text.SubString(0, Count)
End Function

HTH,
Mythran
Lebesgue - 06 Sep 2005 18:03 GMT
Very funny, posting a VB answer for C# homework :-)

> > Hello, I'm new to c$
> > I need help writing a function that matches a word only if the word is
[quoted text clipped - 24 lines]
> HTH,
> Mythran
Marina - 06 Sep 2005 18:10 GMT
Not to mention, it's not really quite the correct answer, as the second
function will throw an error if the word is shorter then Count...

> Very funny, posting a VB answer for C# homework :-)
>
[quoted text clipped - 27 lines]
>> HTH,
>> Mythran
LogixSR79 - 06 Sep 2005 18:23 GMT
Well funny if you don't realize that the .net framework trancends VB, C# and
C++

For C# it is nearly the same as VB:

And since im lazy this is word for word from MSDN

StartsWith:

[Visual Basic]
Dim MyString As String = "Hello World"
Console.WriteLine(MyString.StartsWith("Hello"))
[C#]
string MyString = "Hello World";
Console.WriteLine(MyString.StartsWith("Hello"));

EndsWith:

[Visual Basic]
Dim MyString As String = "Hello World"
Console.WriteLine(MyString.EndsWith("Hello"))
[C#]
string MyString = "Hello World";
Console.WriteLine(MyString.EndsWith("Hello"));

Now i'm sure you can figure it out from this point LOL
But if you have to do it programmatically on your own then get the length of
the word to test for and skip back that much in the string to test in and
see if it compares true, might want to trim spaces at end, don't know how in
depth it needs to be...

> Very funny, posting a VB answer for C# homework :-)
>
[quoted text clipped - 27 lines]
>> HTH,
>> Mythran
LogixSR79 - 06 Sep 2005 18:24 GMT
> Well funny if you don't realize that the .net framework trancends VB, C#
> and C++

Okay well it is funny considering original post went to C# NG as well :P

> For C# it is nearly the same as VB:
>
[quoted text clipped - 57 lines]
>>> HTH,
>>> Mythran
Mythran - 06 Sep 2005 18:34 GMT
w00ps :)

public bool AtEndOfString(string Text, string Word)
{
   return Text.EndsWith(Word);
}

public string GetBeginningOfString(string Text, int Count)
{
   if (Text != string.Empty && Text != null && Text.Length >= Count) {
       return Text.SubString(0, Count);
   }

   return Text;
}

Sorry about the reply in VB.Net, I'm stuck on VB today :)  Oh yeah, this was
off top of my head, so it's not tested :)

HTH :)

Mythran

> Very funny, posting a VB answer for C# homework :-)
>
[quoted text clipped - 11 lines]
>> HTH,
>> Mythran
billy - 07 Sep 2005 13:27 GMT
> Very funny, posting a VB answer for C# homework :-)

Just curious, why don't you do your own homework? You're doing yourself
more harm than good coming to newsgroups for quick answers and you're
developing bad habits that are most likely going to stick with you your
whole career. This stuff here is very simple and you're setting yourself
up for a tough road ahead if you can't get through it. Sorry for
sounding like a creep but the software development community as whole
already is forgetting how to think for itself and the overabundance and
accessibility of "answers" is making things worse.

~billy
Jon Skeet [C# MVP] - 06 Sep 2005 18:13 GMT
> Hello, I'm new to c$
> I need help writing a function that matches a word only if the word is
[quoted text clipped - 3 lines]
> string b = "google I like";
> string a would return true, b would return false

There's already a method which does that: string.EndsWith.

> secondly i need to write a function that checks if the length of my
> strings is less or equal to 20 characters. if more than 20 chars then
> remove the extra ones.

Use the Length property and the Substring method (both of the string
type).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Brendan Grant - 07 Sep 2005 01:06 GMT
In order to check to see if a string ends with a given substring, simply use
the EndsWith() function... to base it off if your example...

a.EndsWith("google") would return true, while b.EndsWith("google") would
return  false.

As for the second bit of truncating a string to be at most 20 characters,
you can use something like:

     string LimitString(string str)
     {
        if( str.Length <= 20)
        {
           return str;
        }

        return str.Substring(0,20);
     }

Which checks the length of the string (although does not check for it being
null) and returns at most the left 20 characters of it.

Brendan

> Hello, I'm new to c$
> I need help writing a function that matches a word only if the word is
[quoted text clipped - 11 lines]
>
> Howard
Mubashir Khan - 07 Sep 2005 06:02 GMT
try using Regex
these are more powerfull

> Hello, I'm new to c$
> I need help writing a function that matches a word only if the word is
[quoted text clipped - 11 lines]
>
> Howard
Howard - 07 Sep 2005 07:22 GMT
Isn't regex slower.

Thanks for the help guys.
Cor Ligthert [MVP] - 07 Sep 2005 07:30 GMT
Howard,

> Isn't regex slower.

Mostly yes.

Cor
Howard - 07 Sep 2005 07:22 GMT
does anyone know how Regex works? whats the algorithm?
Greg Bacon - 07 Sep 2005 15:01 GMT
: does anyone know how Regex works? whats the algorithm?

For an overview, see "How Regexes Work" at

   http://perl.plover.com/Regex/

Greg
Jon Skeet [C# MVP] - 07 Sep 2005 07:30 GMT
> try using Regex
> these are more powerfull

... and completely over the top in this situation. Using a regular
expression here would be like using a database instead of a hashtable,
just to hold a map of 10 items which doesn't need to be persistent.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.