Ts 字符替换 类似C++ %s 要怎么写

Ts 字符替换 类似C++ %s要怎么写

不用额外写,引擎有这个方法,cc.js.formatStr(“a: %s, b: %s”, a, b);

1赞

感谢~~~~


declare module cc

{

    /** 引擎math拓展 */

    export namespace math {

        export const EPSILON:number;

        /**

         * Tests whether or not the arguments have approximately the same value, within an absolute

         * or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less

         * than or equal to 1.0, and a relative tolerance is used for larger values)

         * 忽略后面6位小数点误差,判断是否相等

         *

         * @param {Number} a The first number to test.

         * @param {Number} b The second number to test.

         * @returns {Boolean} True if the numbers are approximately equal, false otherwise.

         */

        export function equals(a:number, b:number):boolean;

        /**

         * Tests whether or not the arguments have approximately the same value by given maxDiff

         * 给予一个误差值maxDiff,判断是否相等

         * @param {Number} a The first number to test.

         * @param {Number} b The second number to test.

         * @param {Number} maxDiff Maximum difference.

         * @returns {Boolean} True if the numbers are approximately equal, false otherwise.

         */

        export function approx(a:number, b:number, maxDiff:number):boolean;

        /**

         * Clamps a value between a minimum float and maximum float value.

         * 设定val的最大最小范围,返回val的范围值

         * 若val > max,返回max,

         * val < min,返回min,

         * max > val > min,返回val

         * @method clamp

         * @param {number} val

         * @param {number} min

         * @param {number} max

         * @return {number}

         */

        export function clamp(val:number, min:number, max:number):number

        /**

         * Clamps a value between 0 and 1.

         * 设定val的最大最小范围1,0,返回val的范围值

         * 若val > 1,返回1,

         * val < 0,返回0,

         * 1 > val > 0,返回val

         * @method clamp01

         * @param {number} val

         * @return {number}

         */

        export function clamp01(val:number):number

        /**

         * 给予比率ratio,取值范围,返回该比率的范围值

         * return from + (to - from) * ratio;

         * @method lerp

         * @param {number} from

         * @param {number} to

         * @param {number} ratio - the interpolation coefficient

         * @return {number}

         */

        export function lerp(from:number, to:number, ratio:number):number

        /**

        * Convert Degree To Radian

        * 角度转弧度

        * @param {Number} a Angle in Degrees

        */

        export function toRadian(a:number):number

        /**

        * Convert Radian To Degree

        * 弧度转角度

        * @param {Number} a Angle in Radian

        */

        export function toDegree(a:number):number

       

        /**

         * Returns a floating-point random number between min (inclusive) and max (exclusive).

         *

         * @method randomRange

         * @param {number} min

         * @param {number} max

         * @return {number} the random number

         */

        export function randomRange(min:number, max:number):number

        /**

         * Returns a random integer between min (inclusive) and max (exclusive).

         *

         * @method randomRangeInt

         * @param {number} min

         * @param {number} max

         * @return {number} the random integer

         */

        export function randomRangeInt(min:number, max:number):number

        /**

         * Linear congruential generator using Hull-Dobell Theorem.

         *

         * @method pseudoRandom

         * @param {number} seed the random seed

         * @return {number} the pseudo random

         */

        export function pseudoRandom(seed:number):number

        /**

         * Returns a floating-point pseudo-random number between min (inclusive) and max (exclusive).

         *

         * @method pseudoRandomRange

         * @param {number} seed

         * @param {number} min

         * @param {number} max

         * @return {number} the random number

         */

        export function pseudoRandomRange(seed:number, min:number, max:number):number

       

        /**

         * Returns a pseudo-random integer between min (inclusive) and max (exclusive).

         *

         * @method pseudoRandomRangeInt

         * @param {number} seed

         * @param {number} min

         * @param {number} max

         * @return {number} the random integer

         */

        export function pseudoRandomRangeInt(seed:number, min:number, max:number):number

        /**

         * Returns float remainder for t / length

         * 根据范围长度,计算t的周期值

         * return t - Math.floor(t / length) * length;

         * (5,3)=>2;

         * (6,3)=>0;

         * @method repeat

         * @param {number} t time start at 0

         * @param {number} length time of one cycle

         * @return {number} the time wrapped in the first cycle

         */

        export function repeat(t:number, length:number):number

        /**

         * Returns time wrapped in ping-pong mode

         * 根据范围长度,计算t的来回周期周期值

         * (5,3)=>2;

         * (6,3)=>0;

         * @method repeat

         * @param {number} t time start at 0

         * @param {number} length time of one cycle

         * @return {number} the time wrapped in the first cycle

         */

        export function pingPong(t:number, length:number):number

        /**

         * Returns ratio of a value within a given range

         * 跟Lerp相反,给予取值范围和值,返回该值得比率

         * @method repeat

         * @param {number} from start value

         * @param {number} to end value

         * @param {number} value given value

         * @return {number} the ratio between [from,to]

         */

        export function inverseLerp(from:number, to:number, value:number):number

        /**

         * Returns -1, 0, +1 depending on sign of x.

         * 返回v的单位值

         * v为负数返回-1,0返回0,正数返回1

         * @method sign

         * @param {number} v

         */

        export function sign (v:number):number

    }

    export namespace js {

        /**

         * Check the obj whether is number or not

         * If a number is created by using 'new Number(10086)', the typeof it will be "object"...

         * Then you can use this function if you care about this case.

         * @method isNumber

         * @param {*} obj

         * @returns {Boolean}

         */

        export function isNumber(obj):boolean;

        /**

         * Check the obj whether is string or not.

         * If a string is created by using 'new String("blabla")', the typeof it will be "object"...

         * Then you can use this function if you care about this case.

         * @method isString

         * @param {*} obj

         * @returns {Boolean}

         */

        export function isString(obj):boolean;

        /**

         * Copy all properties not defined in obj from arguments[1...n]

         * @method addon

         * @param {Object} obj object to extend its properties

         * @param {Object} ...sourceObj source object to copy properties from

         * @return {Object} the result obj

         */

        export function addon(obj,...args:any[]):any;

        /**

         * copy all properties from arguments[1...n] to obj

         * @method mixin

         * @param {Object} obj

         * @param {Object} ...sourceObj

         * @return {Object} the result obj

         */

        export function mixin(obj,...args:any[]):any;

        export function clear(obj):void;

        export function isEmptyObject(obj):boolean;

        /**

         * A string tool to construct a string with format string.

         * 占位符字符串拼接工具

         * 占位符%s

         * @method formatStr

         * @param {String|any} msg - A JavaScript string containing zero or more substitution strings (%s).

         * @param {any} ...subst - JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.

         * @returns {String}

         * @example

         * cc.js.formatStr("a: %s, b: %s", a, b);

         * cc.js.formatStr(a, b, c);

         */

        export function formatStr(str:string,...args:any[]):string;

        /**

         * Removes the array item at the specified index.

         * 删除指定索引得元素

         * @method removeAt

         * @param {any[]} array

         * @param {Number} index

         */

        export function removeAt(array:any[],index:number):void;

        /**

         * Removes the array item at the specified index.

         * It's faster but the order of the array will be changed.

         * 快速删除指定索引得元素,用最后一个索引元素做填充

         * 会数组元素顺序

         * @method fastRemoveAt

         * @param {any[]} array

         * @param {Number} index

         */

        export function fastRemoveAt (array:any[],index:number):void;

        /**

         * Verify array's Type

         * 验证数组中所有元素是否指定类型

         * @method verifyType

         * @param {array} array

         * @param {Function} type

         * @return {Boolean}

         */

        export function verifyType (array:any[], type):boolean;

        /**

         * Removes from array all values in minusArr. For each Value in minusArr, the first matching instance in array will be removed.

         * array移除所有minusArr中的元素

         * @method removeArray

         * @param {Array} array Source Array

         * @param {Array} minusArr minus Array

         */

        export function removeArray(array:any[],minusArr:any[]):void;

        /**

         * Inserts some objects at index

         * array在指定位置index插入addObjs数组中所有的元素

         * @method appendObjectsAt

         * @param {Array} array

         * @param {Array} addObjs

         * @param {Number} index

         * @return {Array}

         */

        export function appendObjectsAt (array:any[], addObjs:any[], index:number):any[];

        /**

         * Determines whether the array contains a specific value.

         * 判断array数组中是否有元素value

         * @method contains

         * @param {any[]} array

         * @param {any} value

         * @return {Boolean}

         */

        export function contains (array:any[], value):boolean;

       

        /**

         * Copy an array's item to a new array (its performance is better than Array.slice)

         * 浅拷贝数组array

         * @method copy

         * @param {Array} array

         * @return {Array}

         */

         export function copy (array:any):any[]

    }

}

ts接口没暴露出来,你可以找个地方加上这些常用的接口

JS 自带的模板字符串 不行吗

1赞

let a = “嗷绕绕”
let b = `字符串=${a}`

1赞

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。