Testing whether string starts with or end with another string
There are built in methods:
"String".start_with? "S" # true
"String".end_with? "4" # false
How to check if a string starts and ends with specific strings?
This regular expression:
^https://.*/MyTest$
will do what you ask.
^
matches the beginning of the string.
https://
will match exactly that.
.*
will match any number of characters (the *
part) of any kind (the .
part). If you want to make sure there is at least one character in the middle, use .+
instead.
/MyTest
matches exactly that.
$
matches the end of the string.
To verify the match, use:
Regex.IsMatch(str, @"^https://.*/MyTest$");
More info at the MSDN Regex page.
How do you check if string ends with another string in Ada?
A slight simplification of Simon's answer:
function Ends_With (Source, Pattern : String) return Boolean is
begin
return Pattern'Length <= Source'Length
and then Source (Source'Last - Pattern'Length + 1 .. Source'Last) = Pattern;
end Ends_With;
How to check for [ ] at beginning and end of string
There is no need for a regex, use start_with
and end_with
:
s = "[text\nhere]"
puts s.start_with?("[") and s.end_with?("]")
# => true
See Ruby demo
If you need a regex, you may use
/\A\[.*\]\z/m
to check if any string starts with [
and ends with ]
.
Explanation:
\A
- start of a string\[
- a literal[
.*
- 0+ any characters, even incl. a newline due to the/m
modifier)\]
- a closing]
\z
- the very end of the string.
See the regex demo
find whether the string starts and ends with the same word
You can use backreference within regex
^(\w+\b)(.*\b\1$|$)
This would match a string only if it
- starts and ends with the same word
- has a single word
Check if string starts or ends with vbCrLf
Try this
StartsWith - checks the first part of a String.
Dim s As String = "vbCrLf bla bla bla"
If s.StartsWith("vbCrLf") Then
MsgBox("Yes")
End If
EndsWith - checks the last characters of a String.
Dim s As String = "bla bla bla vbCrLf"
If s.EndsWith("vbCrLf") Then
MsgBox("Yes")
End If
Regexp - How to check whether a string starts OR ends with given character?
If you just have one character at the beginning or the end, it's as simple as saying this:
/^!|!$/
No need for a capture group.
Update: To make sure you get either an exclamation mark and ten letters or ten letters and an exclamation mark, build your pattern like this:
/^!?[a-z0-9]{1,10}$|^[a-z0-9]{1,10}!?$/
It looks like it's repeating itself, but it's actually way cheaper and faster than having three capture groups.
I put together a unit test in regex101 for this.
How to check if a string starts with another string in C?
Apparently there's no standard C function for this. So:
bool startsWith(const char *pre, const char *str)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0;
}
Note that the above is nice and clear, but if you're doing it in a tight loop or working with very large strings, it does not offer the best performance, as it scans the full length of both strings up front (strlen
). Solutions like wj32's or Christoph's may offer better performance (although this comment about vectorization is beyond my ken of C). Also note Fred Foo's solution which avoids strlen
on str
(he's right, it's unnecessary if you use strncmp
instead of memcmp
). Only matters for (very) large strings or repeated use in tight loops, but when it matters, it matters.
Checking whether a string starts with XXXX
aString = "hello world"
aString.startswith("hello")
More info about startswith
.
Related Topics
How to Open Files Relative to Home Directory
Get Today's Date in Jekyll with Liquid Markup
How to Load a Spec_Helper.Rb Automatically in Rspec 2
Rails, Ruby, How to Sort an Array
Rails: How to Determine Controller/Action in View
How to Send Mail with Rails Without a Template
How Might I Pass Text Data from the Ruby Console into My Clipboard Without Saving to a File
Add Comment to User and Post Models (Ruby on Rails)
How to Dynamically Define a Class Method Which Will Refer to a Local Variable Outside
Understanding Io.Select When Reading Socket in Ruby
Help Refactoring This Nasty Ruby If/Else Statement
How to Return a Value from a Thread in Ruby
Gem::Loaderror: Specified 'Sqlite3' for Database Adapter, But the Gem Is Not Loaded
Ruby Method Like 'Self' That Refers to Instance
Ruby Remove Empty Lines from String
Find Records with Datetime That Match Today's Date - Ruby on Rails