1.substring Method

Definition and Usage

substring Method is used to extract characters that are mediated between two specified subscripts .

grammar

stringObject.substring(start,stop)

Parameters      describe

start     It's necessary . A nonnegative integer , Specifies that the first character of the substring to be extracted is in stringObject Position in .

stop     Optional . A nonnegative integer , The last character of the substring to be extracted is in stringObject There are many places in 1. If this parameter is omitted , Then the returned substring will go all the way to the end of the string .

Return value

A new string , The string value contains stringObject A substring of , Its content is from start Go to stop-1 All characters at , The length of stop reduce start.

explain

substring The substring returned by the method includes start The character at , But does not include end The character at .

If start And end equal , Then the method returns an empty string ( The length is 0 String ).

If start Than end Big , Then the method will exchange these two parameters before extracting the substring .

If start or end It's a negative number , It will be replaced by 0.

2.substr Method

Definition and Usage

substr Method is used to return a substring of a specified length starting at a specified position .

grammar

stringObject.substr(start [, length ])

Parameters     describe

start   It's necessary . The starting position of the required substring . The index of the first character in the string is 0.

length Optional . The number of characters that should be included in the returned substring .

explain

If length by 0 Or negative numbers , Will return an empty string .

If the parameter is not specified , Then the substring will continue to stringObject Last .

give an example :

var str = "0123456789";

alert(str.substring(0));------------"0123456789"

alert(str.substring(5));------------"56789"

alert(str.substring(10));-----------""

alert(str.substring(12));-----------""

alert(str.substring(-5));-----------"0123456789"

alert(str.substring(-10));----------"0123456789"

alert(str.substring(-12));----------"0123456789"

alert(str.substring(0,5));----------"01234"

alert(str.substring(0,10));---------"0123456789"

alert(str.substring(0,12));---------"0123456789"

alert(str.substring(2,0));----------"01"

alert(str.substring(2,2));----------""

alert(str.substring(2,5));----------"234"

alert(str.substring(2,12));---------"23456789"

alert(str.substring(2,-2));---------"01"

alert(str.substring(-1,5));---------"01234"

alert(str.substring(-1,-5));--------""

alert(str.substr(0));---------------"0123456789"

alert(str.substr(5));---------------"56789"

alert(str.substr(10));--------------""

alert(str.substr(12));--------------""

alert(str.substr(-5));--------------"0123456789"

alert(str.substr(-10));-------------"0123456789"

alert(str.substr(-12));-------------"0123456789"

alert(str.substr(0,5));-------------"01234"

alert(str.substr(0,10));------------"0123456789"

alert(str.substr(0,12));------------"0123456789"

alert(str.substr(2,0));-------------""

alert(str.substr(2,2));-------------"23"

alert(str.substr(2,5));-------------"23456"

alert(str.substr(2,12));------------"23456789"

alert(str.substr(2,-2));------------""

alert(str.substr(-1,5));------------"01234"

alert(str.substr(-1,-5));-----------""

source : Big test -Java authentication

java substring and substr More articles about

  1. C++ substr() and Java substring() difference

    Java and C++ There are operations on substrings in ,C++ Medium is substr(),Java Medium is substring(), There are some differences in usage between the two , First, in the case of only one parameter : s.substr(start) and s. ...

  2. substring() and substr() The use and difference of

    stay JavaScript in , Interception is often used , What about interception , In fact, it is to obtain the content from a certain location to a certain location of the intercepted element , that JS Provided me with substring and substr These two methods : What's the difference between these two interception methods ...

  3. JavaScript in , Extraction substring method :Slice、Substring、Substr Comparison .

    stay JavaScript in , Substrings are extracted mainly through  Slice.Substring.Substr One of the three ways . // slice // grammar : string.slice(beginSlice [, e ...

  4. js in substring And substr Learning from .

    Today in the process of working , notice js Two twin functions in . Namely substring And substr. I was immediately bewildered by two hateful guys , I don't know what the function is .. First look at it. substring How is the manual introduced . The manual explains ...

  5. javascript String interception substring、substr and slice

    This article introduces in detail javascript in substring().substr() and slice() Three JS String interception method ,substring() Method is used to extract characters that are mediated between two specified subscripts .subst ...

  6. js String truncation function slice()、substring()、substr()

    Abstract stay js There are three commonly used character interception functions in slice().substring().substr() 了 , Now let me introduce slice().substring().substr() Some usage of function in character interception and ...

  7. javascript in substring()、substr()、slice() The difference between

    stay js There are three commonly used character interception functions slice().substring().substr() 了 , Now let me introduce slice().substring().substr() Some usage and difference of function in character interception . ...

  8. js in substring and substr Usage of

    js in substring and substr Usage of substring Method is used to extract characters that are mediated between two specified subscripts substring(start,end) Where to start and where to end , Index from scratch Parameters    ...

  9. substring and substr、$.extend()、$.fn.extend()、(function($){….})(jQuery) Simple explanation of

    1.    JS in substring And substr The difference between Substring: The method can have one or two parameters . l  One parameter : Example : var str="Olive": ...

Random recommendation

  1. php Two dimensional arrays are the same id Add up the number of

    Array styles $arr = array( array( , , , ), array( , , , ), array( , , , ), array( , , , ), ); Treatment method $item=arr ...

  2. yii2 Sign in 、 sign out 、 automatic logon

    The principle of automatic login is very simple . The main thing is to make use of cookie In the first login , If login is successful and auto login next time is selected , Then the user's authentication information will be saved to cookie in ,cookie Valid for 1 Years or months . Under ...

  3. HTML The paragraph

    You can put HTML The document is divided into paragraphs . HTML The paragraph The passage is through <p> The label defines . example <p>This is a paragraph</p> <p&g ...

  4. 【LeetCode】6 - ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  5. A good one PHP Verification code class

    Share a good php Verification code class , Including call examples . explain : If the specified font does not apply , Then use imagestring() function , If you need to encounter the specified font , You use imagettftext() function . The position of the font is C Under the plate ...

  6. C# Programming to realize Excel Search for text in a document

    Yes, it is Word Document programming to achieve the experience of searching text , stay Excel It's not difficult to implement this function in the . open Excel Of VBA help , see Excel Object model of , It's easy to find several sets and objects needed to complete this function :Applicati ...

  7. understand JAVA Memory model

    actually java The memory model is as shown in the figure above Each thread has its own stack memory , Store copies of shared objects , The local variable Each thread's own local variables are invisible , But shared objects are visible to every thread . If you want to achieve thread communication , Thread pair sharing ...

  8. Nginx And ( 3、 ... and )Nginx To configure

    A simple configuration file is as follows : # Definition Nginx Running users and user groups user userName userGroupName; # Number of work processes , Adjust according to the hardware , Usually equal to CPU Quantity or 2 Twice as much as CPU worker_ ...

  9. java History to learn

    1991 year ,Sun The company's Green project ,Oak 1995 year , Introduction Java The beta 1996 year ,JDK1.0 1997 year ,JDK1.1 1998 year ,JDK1.2, Greatly improved defects in earlier versions , It's a revolutionary version ...

  10. Multi objective genetic algorithm ------ NSGA-II ( Partial source code analysis ) The set of real Numbers 、 Mutation operation of binary code mutation.c

    Mutation operation of genetic algorithm /* Mutation routines */ # include <stdio.h> # include <stdlib.h> # include < ...