String 변수를 활용하여 다양한 작업을 할 수 있는데요.

오늘은 String을 활용하는 내용을 정리해볼까 합니다.

(두구두구)  🥁🥁🥁

 

 

문자열 찾기 / 문자열 변환 하기 (indexOf, replace) 살펴보기 

 

 

[Android] Kotlin String 문자열 찾기, 문자열 변경 치환 (indexOf, replace)

String 변수를 활용하여 다양한 작업을 할 수 있는데요. 오늘은 String을 활용하는 내용을 정리해볼까 합니다. (두구두구) 🥁🥁🥁 문자열 자르기 / 문자열 분할 하기 (substring, split, chunked) 살펴보기

salmonpack.tistory.com

 

Kotlin String 문자열 활용하기 - 대소문자 변환, format, 공백제거 (%d, \n, trim ...)

오늘은 간단하게 String에 대하여 말해볼까 합니다. (가장 만만한 게 또 String이네요... 😭😭) String 관련하여 %d 또는 %s 등의 포맷을 사용하는걸 한 번쯤 보신 적 있으실 겁니다. 또는 String 공백 제

salmonpack.tistory.com


String.substring

문자열 자르기

 

public fun String.substring(range: IntRange): String = substring(range.start, range.endInclusive + 1)
public actual inline fun String.substring(startIndex: Int): String = (this as java.lang.String).substring(startIndex)
public actual inline fun String.substring(startIndex: Int, endIndex: Int): String = (this as java.lang.String).substring(startIndex, endIndex)

 

위와 같이 substring 파라미터 값에 따라 범위 적용 가능합니다.

 


 

substring 결과 값

 

str.substring(0 until 5)

> str 문자열의 0번째부터 5번째 전까지의 문자열 자르기

 

str.substring(5)

> startIndex(5번째)에서 시작하여 문자열 끝까지 자르기 

 

str.substring(5, 9)

> startInedx(5번째)부터 시작하여 endInext(9번째) 전까지

 


String.split

문자열 분할 / 문자열 나누기 / 문자열 쪼개기?

 

public fun CharSequence.split(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0): List<String>

/* 
	Params:
	delimiters - One or more strings to be used as delimiters.
	ignoreCase - true to ignore character case when matching a delimiter. 
    By default false.
	limit -
	The maximum number of substrings to return. Zero by default means no limit is set.
	To avoid ambiguous results when strings in delimiters have characters in common, 
    this method proceeds from the beginning to the end of this string, 
    and matches at each position the first element in delimiters 
    that is equal to a delimiter in this instance at that position.
*/

 

split를 활용하여 문자를 분할하고 결과 값은 List <String> 값으로 반환됩니다.

 


split 결과 값

 

str.split("*", "/")

> * ,  /  두 가지 문자를 기준으로 문자열을 분할  

 

str.split("*")

> * 기준으로 문자열을 분할 

 

str.split("*", limit =2)

> * 기분으로 최대 2개로 분할

 

 


String.chunked

문자열 분할 / 문자열 나누기 / 문자열 쪼개기?

 

public fun CharSequence.chunked(size: Int): List<String>

chunked를 활용하여 문자열을 크기별로 분할하고 결과 값은 List 값으로 반환됩니다.

 


chunked 결과 값

 

 


여기까지 저의 긴 글을 읽어주셔서 감사합니다.

제가 습관적으로 코딩을 하는 그날까지 습관적으로 코딩을 하기 위해 글 작성을 꾸준하게 해보겠습니다.

 

 

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기