The R Project SVN R

Rev

Rev 70075 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 70075 Rev 77195
Line 4... Line 4...
4
\alias{startsWith}
4
\alias{startsWith}
5
\description{
5
\description{
6
  Determines if entries of \code{x} start or end with string (entries of)
6
  Determines if entries of \code{x} start or end with string (entries of)
7
  \code{prefix} or \code{suffix} respectively, where strings are
7
  \code{prefix} or \code{suffix} respectively, where strings are
8
  recycled to common lengths.
8
  recycled to common lengths.
9
 
-
 
10
  \code{startsWith()} is equivalent to but much faster than
-
 
11
  \preformatted{substring(x, 1, nchar(prefix)) == prefix}
-
 
12
  or also \preformatted{grepl("^<prefix>", x)} where \code{prefix} is
-
 
13
  not to contain special regular expression characters.
-
 
14
}
9
}
15
\usage{
10
\usage{
16
startsWith(x, prefix)
11
startsWith(x, prefix)
17
  endsWith(x, suffix)
12
  endsWith(x, suffix)
18
}
13
}
Line 20... Line 15...
20
  \item{x}{vector of \code{\link{character}} string whose \dQuote{starts}
15
  \item{x}{vector of \code{\link{character}} string whose \dQuote{starts}
21
    are considered.}
16
    are considered.}
22
  \item{prefix, suffix}{\code{\link{character}} vector (often of length one).}
17
  \item{prefix, suffix}{\code{\link{character}} vector (often of length one).}
23
}
18
}
24
\details{
19
\details{
-
 
20
  \code{startsWith()} is equivalent to but much faster than
-
 
21
\preformatted{  substring(x, 1, nchar(prefix)) == prefix  }
-
 
22
  or also
-
 
23
\preformatted{  grepl("^<prefix>", x)  }
-
 
24
  where \code{prefix} is not to contain special regular expression
-
 
25
  characters (and for \code{grepl}, \code{x} does not contain missing
-
 
26
  values, see below).
-
 
27
 
25
  The code has an optimized branch for the most common usage in which
28
  The code has an optimized branch for the most common usage in which
26
  \code{prefix} or \code{suffix} is of length one, and is further
29
  \code{prefix} or \code{suffix} is of length one, and is further
27
  optimized in a UTF-8 or 8-byte locale if that is an ASCII string.
30
  optimized in a UTF-8 or 8-byte locale if that is an ASCII string.
28
}
31
}
29
\value{
32
\value{
Line 50... Line 53...
50
      startsWith(x1, "bla"),
53
      startsWith(x1, "bla"),
51
        endsWith(x1, "n"),
54
        endsWith(x1, "n"),
52
        endsWith(x1, "an"))
55
        endsWith(x1, "an"))
53
rownames(x2) <- x1; colnames(x2) <- c("b", "b1", "bla", "n", "an")
56
rownames(x2) <- x1; colnames(x2) <- c("b", "b1", "bla", "n", "an")
54
x2
57
x2
-
 
58
 
-
 
59
## Non-equivalence in case of missing values in 'x', see Details:
-
 
60
x <- c("all", "but", NA_character_)
-
 
61
cbind(startsWith(x, "a"),
-
 
62
      substring(x, 1L, 1L) == "a",
-
 
63
      grepl("^a", x))
55
}
64
}
56
\keyword{character}
65
\keyword{character}
57
\keyword{utilities}
66
\keyword{utilities}