[Javascript] InStr in JavaScript

asp에서 쓰던 mid 와 instr을 자바스크립트 함수로 만든것

Mid(string, start, length): Returns a specified number of characters from a string

IN: str - the string we are LEFTing
      start - our string's starting position (0 based!!)
      len - how many characters from start we want to get
RETVAL: The substring from start to start+len

<script language="JavaScript">
function Mid(str, start, len)
{
// Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}
</script>

Keep in mind that strings in JavaScript are zero-based, so if you ask for Mid("Hello",1,1), you will get "e", not "H".   To get "H", you would simply type in Mid("Hello",0,1)

You can alter the above function so that the string is one-based. Just check to make sure start is not <= 0, alter the iEnd = start + len to

iEnd = (start - 1) + len, and in your final return statement, just return ...substring(start-1,iEnd)



InStr(str, SearchForStr): Returns the location a character (charSearchFor) was found in the string str

InStr function written by: Steve Bamelis
InStr(strSearch, charSearchFor) : Returns the first location a substring (charSearchFor) found in the string strSearch.   (If the character is not found, -1 is returned.)
                                       
<script language="JavaScript">
function InStr(strSearch, charSearchFor)
{
            for (i=0; i < strSearch.length; i++)
            {
                  if (charSearchFor == Mid(strSearch, i, 1))
                  {
                        return i;
                  }
            }
            return -1;
}
</script>


Example:
alert(InStr("Hello_World", "_"))

Output:
5



출처 : http://www.expertsforge.com
크리에이티브 커먼즈 라이센스
Creative Commons License
이 글의 관련글
    이글의 태그와 관련된 글이 없습니다.

Posted by web20korea

2008/01/17 14:00 2008/01/17 14:00

function Month_Day(i_year, i_month)
      dim now_first_date            : now_first_date = i_year &"-"& RIGHT("0"& i_month,2) &"-01"
      dim next_first_date            : next_first_date = DateAdd("m",1,now_first_date)
      dim now_last_date            : now_last_date = DateAdd("d",-1,next_first_date)
      dim now_month_days            : now_month_days = Day(now_last_date)
      Month_Day = now_month_days
end function
크리에이티브 커먼즈 라이센스
Creative Commons License
이 글의 관련글

Posted by web20korea

2007/12/07 17:33 2007/12/07 17:33

function Re_Date(dates, s_date, e_date)
 s = int(s_date) '구하려는 시작일 1-일, 2-월, 3-화, 4-수, 5-목, 6-금, 7-토
 e = int(e_date) '구하려는 끝일 1-일, 2-월, 3-화, 4-수, 5-목, 6-금, 7-토
 w = DatePart("w",dates)
 dates = CDate(dates)
   re_s = DateAdd("d", dates , s - w)
   re_e = DateAdd("d", dates , e - w)
 Re_Date = "시작일 : " & re_S & "<br>끝일 : " & re_e
end function

Response.Write Re_Date("2007-09-13",1,7)

크리에이티브 커먼즈 라이센스
Creative Commons License
이 글의 관련글

Posted by web20korea

2007/10/15 18:23 2007/10/15 18:23


블로그 이미지

web20korea's

- web20korea

Site Stats

Total hits:
53621
Today:
149
Yesterday:
213