string question

H

Howard

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
 
M

Mythran

Howard said:
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

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
 
M

Marina

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

Jon Skeet [C# MVP]

Howard said:
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

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

LogixSR79

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

LogixSR79

LogixSR79 said:
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:

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


Lebesgue said:
Very funny, posting a VB answer for C# homework :)
 
M

Mythran

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
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

Mubashir Khan said:
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.
 
B

billy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top