Indexof (string. indexof method)

Source: Internet
Author: User
Indexof (string. indexof method)

The indexof () method of a string searches for whether a string is passed as a parameter. If a string is found, the start position (0 indicates the first character) of the character is returned, 1 indicates the second character and so on.) If not found,-1 is returned.

 

ReturnStringThe first occurrence of a substring in an object.

Public indexof (value: String, [startindex: Number]): Number

Search for strings and return within the called stringsStartindexTheValueThe position of the first matching item. This index starts from scratch, meaning that the first character in the string is considered to be at index 0 rather than Index 1. If not foundValue, This method returns-1.

Parameters

Value: String-A string; the substring to be searched.

Startindex: Number[Optional]-an integer that specifies the start index of the search.

Return

Number-The position of the first match of the substring, or-1.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Indexof Method
Returns the first occurrence of a substring in a string object.

Strobj. indexof (substring [, startindex])

Parameters
Strobj

Required. String object or text.

Substring

Required. The substring to be searched in the string object.

Starindex

Optional. This integer indicates the index in the string object. If it is omitted, It is searched from the beginning of the string.

Description
The indexof method returns an integer indicating the starting position of the substring in the string object. If no substring is found,-1 is returned.

If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.

Search from left to right. Otherwise, this method is the same as lastindexof.

Example
The following example illustrates how to use the indexof method.

Function indexdemo (str2 ){
VaR str1 = "babebibobubabebibobu"
VaR S = str1.indexof (str2 );
Return (s );
}

Instance:

I get a string named "1, 18, 33"
If it is written as a indexof ("1"), it seems that it cannot be found. More importantly, there is a 1 in front of 18 and 1, so the established conditions are inaccurate. How can I write them?

 

Indexof

String test = "1, 18, 33 ";
If (test. indexof ("1")>-1)
{
Response. Write ("exist ");
}
Else
{
Response. Write ("nonexistent ");
}

However, if only 1 meets the requirements, and 1 in 18 does not, you cannot use indexof.

Using system. Text. regularexpressions;

String test = "1, 18, 33 ";
If (RegEx. ismatch (test, @ "\ B1 \ B "))
{
Response. Write ("exist ");
}
Else
{
Response. Write ("nonexistent ");
}

Note:

\ B matches a word boundary in the Regular Expression

Write a method

// SRC Source string
// Tar string to be compared
Private bool checkstring (string SRC, string TAR)
{
String temp = RegEx. Replace (TAR, @ "[. $ ^ {\ [(|) * +? \] "," ");
If (temp. Length <tar. length)
Return false;
If (RegEx. ismatch (SRC, @ "\ B" + tar + @ "\ B "))
Return true;
Return false;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.