5

I am trying to check if a string starts with the character: /

How can i accomplish this?

Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • Possible duplicate of [How to check if a string "StartsWith" another string?](http://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string) – Damjan Pavlica Nov 09 '15 at 08:47

7 Answers7

21
if(someString.indexOf('/') === 0) {
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
11

Characters of a string can be accessed through the subscript operator [].

if (string[0] == '/') {

}

[0] means the first character in the string as indexing is 0-based in JS. The above can also be done with regular expressions.

David G
  • 94,763
  • 41
  • 167
  • 253
  • @JustinNiessner In what way is this unnecessary for you? – David G Mar 09 '13 at 13:02
  • 1
    @JustinNiessner It's fine as long as string is a variable. – Daniel Imms Mar 09 '13 at 13:06
  • yep, since we're just checking for the first index of the string to be `/` and nothing else. – wei2912 Mar 09 '13 at 13:10
  • This is the best answer for *this particular case* since the search string is only 1 character long. – rvighne Jun 19 '14 at 19:05
  • This seems better for this use case (checking if string starts with one particular character). This feature was added in ES5; here a reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Description – Van J. Wilson Oct 04 '15 at 12:09
4

It's 2022 and startsWith has great support

let string1 = "/yay"
let string2 = "nay"

console.log(string1.startsWith("/"))
console.log(string2.startsWith("/"))
Félix Paradis
  • 5,165
  • 6
  • 40
  • 49
2

Alternative to String.indexOf: /^\//.test(yourString)

KooiInc
  • 119,216
  • 31
  • 141
  • 177
1
data.substring(0, input.length) === input

See following sample code

var data = "/hello";
var input = "/";
if(data.substring(0, input.length) === input)
    alert("slash found");
else 
    alert("slash not found");

Fiddle

Umair Saleem
  • 1,055
  • 5
  • 19
  • Please check i have modified the answer and in my opinion its efficient. Up vote it if you like that. – Umair Saleem Mar 09 '13 at 13:09
  • If I am right this doesn't actually solve the problem. Please reread the question :) – wei2912 Mar 09 '13 at 13:11
  • It solves the problem..Check this fiddle [link](http://jsfiddle.net/umairsaleemid/kXY97/) – Umair Saleem Mar 09 '13 at 13:17
  • This does solve the problem, albeit probably not in the best way. Doesn't deserve a DV though. – c24w Mar 09 '13 at 13:17
  • 1
    +1 because this route is the only one that can easily be extended to be an efficient `startsWith` for large strings (even though I prefer [`slice`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/slice) to `substring`). – Paul S. Mar 09 '13 at 13:23
  • 2
    If efficient extensibility is the goal, it [looks like](http://jsperf.com/javascript-string-begin-with-indexof-vs-substr/2) you're better off using `substr`. – c24w Mar 09 '13 at 13:35
0
<script>
   function checkvalidate( CheckString ) {
      if ( CheckString.indexOf("/") == 0 ) 
        alert ("this string has a /!");
   }
</script>

<input type="text" id="textinput" value="" />
<input type="button" onclick="checkvalidate( document.getElementById('textinput').value );" value="Checkme" />
MackieeE
  • 11,751
  • 4
  • 39
  • 56
  • My apologies =) Edited! I'll read the question properly before rushing off to writing an answer :)! – MackieeE Mar 09 '13 at 13:07
0
var str = "abcd";

if (str.charAt(0) === '/')
David G
  • 94,763
  • 41
  • 167
  • 253
Amrendra
  • 2,019
  • 2
  • 18
  • 36