잠시만 기다려 주세요

     '왜 이재명은 자꾸만 국민들 보고 길바닥에 나가라고 하는 건가.. 정작 당신들은 뜨뜻한 곳에서 입만 나불거리고 있으면서...'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (19)  |  여러가지 팁 (1095)  |  추천 및 재미 (163)  |  자료실 (28)  |  
시사, 이슈, 칼럼, 평론, 비평 (791)  |  끄적거림 (142)  |  문예 창작 (719)  |  바람 따라 (75)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    javascript/jquery

javascript/jquery - 자바스크립트 substring, substr, indexOf, lastIndexOf (javascript 문자열 자르기, 뒤에서 자르기, 찾기)
이 름 : 바다아이   |   조회수 : 10531         짧은 주소 : https://www.bada-ie.com/su/?461591914157
자바스크립트는 문자열의 시작위치를 0부터 순차적으로 인식 합니다.

substring([시작위치], [종료위치]);

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <body>
    <script>
      var str = 'huskdoll.tistory.com';
      document.write( '<p>'+str+'</p>' );
      document.write( '<p>str.substring( 9, 16 ) : ' + str.substring( 9, 16 ) + '</p>' );
    </script>
  </body>
</html>

substr([시작위치], [길이]);

substr 를 이용하여 자르면 substring( 9, 7 )

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <body>
    <script>
      var str = 'huskdoll.tistory.com';
      document.write( '<p>'+str+'</p>' );
      document.write( '<p>str.substr( 9, 7 ) : ' + str.substr( 9, 7 ) + '</p>' );
    </script>
  </body>
</html>

위에서 보셨듯이 substring는 추출하려는 문자열의 시작위치와 종료위치를 입력하여 추출하는 방식입니다.

substr 같은 경우는 시작위치를 입력하고 그 시작 위치부터 지정한 문자열 길이만큼 추출하는 방식입니다.

* 추가로 뒤에서 자르는 방식은 다음과 같습니다.

뒤부터 자르는 방법 : substr( str.length-3, 3 )

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <body>
    <script>
      var str = 'huskdoll.tistory.com';
      document.write( '<p>'+str+'</p>' );
      document.write( '<p>str.substr( str.length-3, 3 ) : ' + str.substr( str.length-3, 3 ) + '</p>' );
    </script>
  </body>
</html>


문자열의 길이에서 자르려는 수의 문자열 위치를 구하고 그 수 만큼 잘라주면 됩니다.

위 함수들과 같이 많이 쓰이는 함수는 indexOf 와 lastIndexOf 입니다.

두개 모두 특정 문자열의 시작위치를 구하는 함수 입니다.

indexOf 는 앞에서 부터 찾고 lastIndexOf 는 뒤에서 찾는것이 차이점 입니다.

huskdoll.tistory.com 문자열에서 . (마침표) 
부분의 위치를 indexOf 와 lastIndexOf 을 이용하여 추출하는 방식을 알려 드리겠습니다.

indexOf([검색 문자열]);

str.indexOf( "." ) 의 결과는 8

lastIndexOf([검색 문자열]);

str.lastIndexOf( "." ) 의 결과는 16

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <body>
    <script>
      var str = 'huskdoll.tistory.com';
      document.write( '<p>'+str+'</p>' );
      document.write( '<p>str.indexOf( "." ) : ' + str.indexOf( "." ) + '</p>' );
      document.write( '<p>str.lastIndexOf( "." ) : ' + str.lastIndexOf( "." ) + '</p>' );
    </script>
  </body>
</html>

위에서 보셨듯이  indexOf는 문자열의 앞에서 부터 검색어를 찾아 시작위치를 알려줍니다.

lastIndexOf 는 문자열의 뒤에서 부터 검색어를 찾아 시작위치를 알려줍니다.

* 찾는 검색어가 없으면 -1 을 리턴합니다.

* 추가로 앞과 끝의 마침표 사이의 문자열을 추출해보도록 하겠습니다.

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <body>
    <script>
      var str = 'huskdoll.tistory.com';
      document.write( '<p>'+str+'</p>' );
      document.write( '<p>마침표 사이 문자열: ' + str.substring(str.indexOf(".")+1, str.lastIndexOf(".")) + '</p>' );
    </script>
  </body>
</html>


* str.indexOf(".")+1 를 해준이유는 검색어의 시작위치이기 때문에 마침표 다음 위치를 추출하기 위해 +1 해주었습니다.


출처 : http://huskdoll.tistory.com/817
| |





      1 page / 3 page
번 호 카테고리 제 목 이름 조회수
77 javascript/jquery .... 바다아이 328
76 javascript/jquery , . 바다아이 8257
75 javascript/jquery , . , 바다아이 6447
74 javascript/jquery javascript , ... 바다아이 8305
73 javascript/jquery , cookie class 바다아이 8920
72 javascript/jquery select 3 바다아이 9897
71 javascript/jquery , ... 바다아이 9157
70 javascript/jquery , timezone, , ... moment.js 바다아이 11869
69 javascript/jquery textarea cursor . focus, cursor ... 바다아이 11577
68 javascript/jquery (block) , ... 바다아이 9459
67 javascript/jquery textarea cursor , , focus 바다아이 12992
66 javascript/jquery jquery ajax option .... 바다아이 10004
65 javascript/jquery jquery open api , ajax JSONP cross domain , , error 0 ... sop 바다아이 9620
64 javascript/jquery javascript , , , , () 바다아이 10614
63 javascript/jquery javascript (date ) 바다아이 10754
62 javascript/jquery CSS3 javascript 바다아이 11437
61 javascript/jquery javascript , cookie, , , , , 바다아이 10522
60 javascript/jquery javascript 바다아이 11243
59 javascript/jquery javascript / , , , 바다아이 14858
58 javascript/jquery url , , encode, decode ... 바다아이 9697
57 javascript/jquery javascript ... frame location.href 바다아이 15377
56 javascript/jquery File Upload Progress, .... . 바다아이 10203
55 javascript/jquery javascript, json , json Highlight 바다아이 10188
54 javascript/jquery javascript json , , JSON.stringify, JSON.parse, 바다아이 11667
53 javascript/jquery javascript innerHTML, innerTEXT ... 바다아이 10194
52 javascript/jquery javascript entity , , encode, decode 바다아이 10506
51 javascript/jquery javascript post, get , 바다아이 12464
50 javascript/jquery text copy, , How to copy a TEXT to Clipboard on a Button-Click 바다아이 10562
49 javascript/jquery jquery autocomplete , , . 바다아이 10195
48 javascript/jquery javascript, jquery, , autocomplete 바다아이 11228
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.42641
to webmaster... gogo sea. gogo sea.