473,785 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string question

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

Sep 6 '05 #1
15 1478

"Howard" <ho*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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(B yVal Text As String, ByVal Word As String) As
Boolean
Return Text.EndsWith(W ord)
End Function

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

HTH,
Mythran

Sep 6 '05 #2
Very funny, posting a VB answer for C# homework :-)

"Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .

"Howard" <ho*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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(B yVal Text As String, ByVal Word As String)

As Boolean
Return Text.EndsWith(W ord)
End Function

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

HTH,
Mythran

Sep 6 '05 #3
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...

"Lebesgue" <no****@spam.jp > wrote in message
news:up******** ******@TK2MSFTN GP14.phx.gbl...
Very funny, posting a VB answer for C# homework :-)

"Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .

"Howard" <ho*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
> 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(B yVal Text As String, ByVal Word As String)

As
Boolean
Return Text.EndsWith(W ord)
End Function

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

HTH,
Mythran


Sep 6 '05 #4
Howard <ho*******@yaho o.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 6 '05 #5
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.WriteLi ne(MyString.Sta rtsWith("Hello" ))
[C#]
string MyString = "Hello World";
Console.WriteLi ne(MyString.Sta rtsWith("Hello" ));

EndsWith:

[Visual Basic]
Dim MyString As String = "Hello World"
Console.WriteLi ne(MyString.End sWith("Hello"))
[C#]
string MyString = "Hello World";
Console.WriteLi ne(MyString.End sWith("Hello")) ;

Now i'm sure you can figure it out from this point LOL
But if you have to do it programmaticall y 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" <no****@spam.jp > wrote in message
news:up******** ******@TK2MSFTN GP14.phx.gbl...
Very funny, posting a VB answer for C# homework :-)

"Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .

"Howard" <ho*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
> 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(B yVal Text As String, ByVal Word As String)

As
Boolean
Return Text.EndsWith(W ord)
End Function

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

HTH,
Mythran


Sep 6 '05 #6

"LogixSR79" <LogixSR79@AT@y ahoo.DOT.com> wrote in message
news:cgkTe.1165 00$Ep.94459@lak eread02...
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.WriteLi ne(MyString.Sta rtsWith("Hello" ))
[C#]
string MyString = "Hello World";
Console.WriteLi ne(MyString.Sta rtsWith("Hello" ));

EndsWith:

[Visual Basic]
Dim MyString As String = "Hello World"
Console.WriteLi ne(MyString.End sWith("Hello"))
[C#]
string MyString = "Hello World";
Console.WriteLi ne(MyString.End sWith("Hello")) ;

Now i'm sure you can figure it out from this point LOL
But if you have to do it programmaticall y 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" <no****@spam.jp > wrote in message
news:up******** ******@TK2MSFTN GP14.phx.gbl...
Very funny, posting a VB answer for C# homework :-)

"Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .

"Howard" <ho*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
> 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(B yVal Text As String, ByVal Word As
String)

As
Boolean
Return Text.EndsWith(W ord)
End Function

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

HTH,
Mythran



Sep 6 '05 #7
w00ps :)

public bool AtEndOfString(s tring Text, string Word)
{
return Text.EndsWith(W ord);
}

public string GetBeginningOfS tring(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

"Lebesgue" <no****@spam.jp > wrote in message
news:up******** ******@TK2MSFTN GP14.phx.gbl...
Very funny, posting a VB answer for C# homework :-)
Public Function AtEndOfString(B yVal Text As String, ByVal Word As String)

As
Boolean
Return Text.EndsWith(W ord)
End Function

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

HTH,
Mythran



Sep 6 '05 #8
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("goo gle") would return true, while b.EndsWith("goo gle") 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(str ing 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
"Howard" wrote:
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

Sep 7 '05 #9
try using Regex
these are more powerfull

"Howard" <ho*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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

Sep 7 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1460
by: sparks | last post by:
I know this is a stupid question but I can't find the answer. Please tell me where to find reference to this so I can print it out and staple it to my head.. dim I as integer dim missedstr as string For i = 1 To 17 'If IsNull("question" & i & ".Value") Then missedstr = missedstr & " question " & i
5
3021
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0" I want this to be @"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0" to treat the escape characters as literals
5
12460
by: Dave | last post by:
I'm receiving info from a com port into a string. I gradually process the string which constantly shortens it. The question is how long can a string be before I need to write some info to disk inorder not to loose any information?
32
14899
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
2
8567
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the string. I had it working somewhat, but ran into problems if one of the highlighted words could also be part of an HTML tag (such as 'Table' or 'Border'). What I need is the regex to find the word, but ignore any words that fall between an HTML...
11
17654
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
53
4095
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
2215
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
39
2906
by: sucaba.r | last post by:
I don't know if this is a unique problem, or I'm going about it the wrong way. I currently connect to one of our SQL servers via a priviliged account (by using RUNAS). Works with no problem. I now need the ability to connect to the same SQL server using ASP. I have the following connect string, but I'm not sure how to specify the domain in the string, or is there some other way? <% Set demoConn =...
7
7818
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.