type DropChar<S extends string, C extends string> = S extends `${infer L}${C}${infer R}` ?
DropChar<`${L}${R}`, C> : S
Solution by IsaacYouKorea #35210
type DropChar<S, C extends string> = S extends `${C}${infer Rest}` ? DropChar<Rest, C>
: S extends `${infer First}${C}${infer Rest}` ? DropChar<`${First}${Rest}`, C>
: S extends `${infer First}${C}` ? DropChar<First, C> : S;
Solution by eunsukimme #35209
type DropChar<S, C> = S extends `${infer First}${infer Rest}` ? First extends C ? DropChar<Rest, C> : `${First}${DropChar<Rest, C>}` : ''
Solution by dailyco #35208
type DropChar<S, C> = S extends `${infer F}${C & string}${infer R}` ? DropChar<`${F}${R}`, C> : S;
Solution by wendao-liu #35171
// your answers
type DropChar<S, C, R extends string = ''> = S extends `${infer First}${infer Rest}`
? First extends C
? DropChar<Rest, C, R>
: DropChar<Rest, C, `${R}${First}`>
: R;
Solution by AndreGeng #34756
// 你的答案
type DropChar<S, C, R extends string = ''> =
S extends `${infer A}${infer B}` ?
A extends C ? DropChar<B, C, R> : DropChar<B, C, `${R}${A}`>
: R
Solution by Jayce-liang #34635
type DropChar<S, C extends string> = S extends `${infer First}${C}${infer Rest}` ? `${First}${DropChar<Rest,C>}` : S
Solution by devshinthant #34629
type DropChar<S extends string, C extends string> = S extends `${infer perfix}${C}${infer suffix}` ? DropChar<`${perfix}${suffix}`, C> : S
Solution by ouzexi #34044
// 你的答案
type DropChar<S, C, U extends string = ''> = S extends `${infer F}${infer R}` ? F extends C ? DropChar<R, C, U> : DropChar<R, C, `${U}${F}`> : U;
Solution by HelloGGG #33425
type DropChar<S extends string, C extends string, R extends string = ''> = S extends `${C}${infer L}`
? DropChar<L, C, L>
: S extends `${infer F}${C}${infer L}` ? DropChar<`${F}${L}`, C, `${F}${L}`>
: S extends `${infer F}${C}` ? DropChar<`${F}`, C, `${F}`>
: R;
Solution by ZhipengYang0605 #33059
type DropChar<S, C, Result extends string = ''> = S extends `${infer First}${infer Rest}`
? DropChar<Rest, C, First extends C ? Result : `${Result}${First}`>
: Result;
Solution by PeterPanov #32973
type DropChar<S extends string, C extends string,Return extends string = ""> = S extends `${infer First}${infer Rest}`
? First extends C
? DropChar<`${Rest}`,C, `${Return}`>
: DropChar<`${Rest}`,C, `${Return}${First}`>
: Return
Solution by yuanruixin #32887
// your answers
type DropChar<S, C extends string> = S extends `${infer F}${C}${infer L}`
? DropChar<`${F}${L}`, C>
: S
Solution by pea-sys #32761
처음 풀이
type DropChar<S extends string, C, R extends string = ''> =
S extends `${infer First}${infer Rest}`
? First extends C
? DropChar<Rest, C, R>
: DropChar<Rest, C, `${R}${First}`>
: R
다른 풀이
type DropChar<S, C extends string> = S extends `${infer L}${C}${infer R}` ? DropChar<`${L}${R}`, C> : S;
${infer L}${C}${infer R}
구조를 이용해서 문자열 find의 기능을 할 수 있는 것을 배웠다.
infer L 이 단순히 순서상 맨 앞에 있다고 첫 번째 문자열을 의미하는게 아니라는 것에 놀랐다
Solution by dev-hobin #32488
type DropChar<S, C extends string> = S extends `${infer F}${C}${infer L}`
? DropChar<`${F}${L}`, C>
: S
Solution by nivenice #32397
type DropChar<S extends string, C extends string> = S extends `${infer F}${C}${infer L}` ? DropChar<`${F}${L}`, C> : S;
Solution by gasmg #31959
type DropChar<S, C extends string> =
S extends `${infer A}${C}${infer R}`
? `${A}${DropChar<R, C>}`
: S extends `${C}${infer R}`
? DropChar<R, C>
: S;
Solution by ricky-fn #31840
type DropChar<S, C extends string> =
S extends `${infer First}${C}${infer Rest}`
? `${First}${DropChar<Rest, C>}`
: S
Solution by gearonixx #31133
type DropChar<S, C extends string> = S extends `${infer L}${C}${infer R}` ? DropChar<`${L}${R}`, C> : S;
Solution by HeeYeonKim98 #30839
type IsEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false
type Con2Tuple<S extends string, C extends string, U extends ReadonlyArray<string> = []> = S extends `${ infer F }${ infer R }` ? IsEqual<F, C> extends true ? [...U, ...Con2Tuple<R, C, U>] : [...U, F, ...Con2Tuple<R, C, U>] : [...U]
type Tuple2Con<U extends ReadonlyArray<string>> = U extends [infer F extends string, ...infer R extends ReadonlyArray<string>] ? `${F}${Tuple2Con<R>}` : ''
type DropChar<S extends string, C extends string> = Tuple2Con<Con2Tuple<S, C>>
Solution by Yirujet #30800
type DropChar<S, C extends string> = S extends `${infer F}${C}${infer T}`
? DropChar<`${F}${T}`, C>
: S
Solution by Chan-Yuxi #30758
type DropChar<S, C> = S extends `${infer F}${infer Rest}`
? F extends C
? `${DropChar<Rest, C>}`
: `${F}${DropChar<Rest, C>}`
: S;
Solution by kai-phan #30749
type DropChar<S, C> =
S extends `${infer First}${infer Rest}` ?
First extends C ?
DropChar<Rest,C> :
`${First}${DropChar<Rest,C>}` :
S
Solution by maximallain #30002
复用已有的ReplaceAll
type ReplaceAll<T extends string, From extends string, To extends string> = T extends `${infer R1}${From}${infer R2}` ? `${ReplaceAll<R1, From, To>}${To}${ReplaceAll<R2, From, To>}` :
T extends `${infer R1}${From}` ? `${ReplaceAll<R1, From, To>}${To}` :
T extends `${From}${infer R2}` ? `${To}${ReplaceAll<R2, From, To>}` : T;
type DropChar<T extends string, E extends string> = ReplaceAll<T, E, ''>;
type butterfly = DropChar<'bu tt er fly', ' '>;
Solution by sundial-dreams #29478
type DropChar<S, C, R extends string = ""> = S extends `${infer A}${infer B}`
? `${A extends C ? DropChar<B, C, R> : DropChar<B, C, `${R}${A}`>}`
: R;
Solution by dmytro-shumak #29435
type StrTransUn<S extends string> = S extends `${infer L}${infer E}` ? `${L}` | StrTransUn<E> : S;
type DropString<S, R extends string> = S extends `${infer F}${infer L}` ? F extends StrTransUn<R> ? `${DropString<L, R>}` : `${F}${DropString<L, R>}`: S;
Solution by MrRENGE #29273
type AbsValueParser<A extends string> = A extends `${infer V}${Unit}` ? [V, Unit]: [A, ''];
type PercentageParser<A extends string> = A extends `${infer S extends Sign}${infer R}` ? [S, ...AbsValueParser<R>] : ['', ...AbsValueParser<A>];
Solution by meiriko #29230
type DropChar<
S extends string,
C extends string
> = S extends `${infer Start}${C}${infer Tail}`
? `${Start}${DropChar<Tail, C>}`
: S
Solution by Kying-star #29014
type DropChar<
S extends string,
C extends string
> = S extends `${infer Char}${infer Str}`
? Char extends C
? DropChar<Str, C>
: `${Char}${DropChar<Str, C>}`
: S;
Solution by DoubleWoodLin #28675
type DropChar<S, C> = S extends `${infer F}${infer R}` ? F extends C ? `${DropChar<R, C>}` : `${F}${DropChar<R, C>}` : S
Solution by hajeonghun #28673