Javascript -- String

String Length:

<script>
    var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var sln = txt.length;
</script>

Sepcial Characters:

<script>
    var x = "It\'s alright";
    var y = "We are the so-called \"Vikings\" from the north ";
</script>

Finding a String in a String

  *indexOf() returns the index of the first occurence of a specified text in a string

<script>
   var str="Hello everyone, I am from Dongguan!";
   var pos = str.indexOf("I");
</script>

 

  *lastIndexOf() returns the index of the last occurence of a specified text

<script>
    var str ="I am from Dongguan and Dongguan is famous for Manufacturing";
</script>
View Code

Searching for a String in a String

  *search() searches a string for a specified value and returns the positions of the match:

<script>
    var str = "Please locate where 'locate' occurs!";
    var pos = str.search("locate");
</script>

Extracting String Parts:

  *slice(start, end): extracts a part of a string and returns the extracted part in a new string.

  *substring(start,end):

  *substr(start,length):

Extract String Characters:

  *charAt(position): returns the character at a specified index

  *charCodeAt(position):returns the unicode of the character at a specified index.

Replacing String Content:

  *replace(): replaces a specified value with another value in a string.

<script>
  var str = "Please visit my city Dongguan";
  var newStr = str.replace("Dongguan","Hobart");
</script>

Converting to Upper and Lower Case

  *str.toUpperCase();

  *str.toLowerCase();

Converting a String to an Array:

  *str.split();

<script>
  var txt = "a,b,c,d,e";
  var txtArray = txt.split(",");  // split on commas
</script>

 

holder

posted on 2016-04-22 15:40  yeatschen  阅读(97)  评论(0编辑  收藏  举报

导航