TypeScript(八) number和string

本文详细介绍了TypeScript中的Number对象(包括MAX_VALUE、MIN_VALUE等属性和toExponential等方法)以及String对象(如length、toLocaleLowerCase等方法),并通过实例展示了如何使用这些特性进行字符串和数值操作。
摘要由CSDN通过智能技术生成

1. TypeScript number

1.1. 描述

  Number对象是原始数值的包装对象。

1.2.语法

var num = new Number(value);;

  注意:如果一个参数值不能转换为一个数字,将返回NaN(非数字值)。

1.3. 对象属性

属性描述
MAX_VALUE可表示的最大的数,MAX_VALUE 属性值接近于 1.79E+308。大于 MAX_VALUE 的值代表 “Infinity”。
MIN_VALUE可表示的最小的数,即最接近 0 的正数 (实际上不会变成 0)。最大的负数是 -MIN_VALUE,MIN_VALUE 的值约为 5e-324。小于 MIN_VALUE (“underflow values”) 的值将会转换为 0。
NaN非数字值(Not-A-Number)
NEGATIVE_INFINITY负无穷大,溢出时返回该值。该值小于 MIN_VALUE。
POSITIVE_INFINITY正无穷大,溢出时返回该值。该值大于 MAX_VALUE。
prototypeNumber 对象的静态属性。使您有能力向对象添加属性和方法。
constructor返回对创建此对象的 Number 函数的引用。

实例:

console.log("TypeScript Number 属性: "); 
console.log("最大值为: " + Number.MAX_VALUE); 
console.log("最小值为: " + Number.MIN_VALUE); 
console.log("负无穷大: " + Number.NEGATIVE_INFINITY); 

执行结果:
在这里插入图片描述

1.4. NaN实例

var month = 0 
if( month<=0 || month >12) { 
    month = Number.NaN 
    console.log("月份是:"+ month) 
} else { 
    console.log("输入月份数值正确。") 
}

执行结果:月份是:NaN

1.5. Number对象方法

1.5.1. toExponential()

  把对象的值转换为指数计数法。

//toExponential() 
var num1 = 1225.30 var val = num1.toExponential(); 
console.log(val) // 输出: 1.2253e+3

1.5.2. toFixed()

  把数字转换为字符串,并对小数点指定位数。

var num3 = 177.234 
console.log("num3.toFixed() 为 "+num3.toFixed())    // 输出:177
console.log("num3.toFixed(2) 为 "+num3.toFixed(2))  // 输出:177.23
console.log("num3.toFixed(6) 为 "+num3.toFixed(6))  // 输出:177.234000

1.5.3. toLocaleString()

  把数字转换为字符串,使用本地数字格式顺序。

var num = new Number(177.1234); 
console.log( num.toLocaleString());  // 输出:177.1234

1.5.4. toPrecision()

  把数字格式化为指定的长度。

var num = new Number(7.123456); 
console.log(num.toPrecision());  // 输出:7.123456 
console.log(num.toPrecision(1)); // 输出:7
console.log(num.toPrecision(2)); // 输出:7.1

1.5.5. toString()

  把数字转换为字符串,使用指定的基数。数字的基数是 2 ~ 36 之间的整数。若省略该参数,则使用基数 10。

var num = new Number(10); 
console.log(num.toString());  // 输出10进制:10
console.log(num.toString(2)); // 输出2进制:1010
console.log(num.toString(8)); // 输出8进制:12

1.5.6. valueOf()

  返回一个 Number 对象的原始数字值。

var num = new Number(10); 
console.log(num.valueOf()); // 输出:10

2. TypeScript string

2.1. 描述

  String对象用于处理文本(字符串)。

2.2. 语法:

var txt = new String(“string”);
// 或更简单方式 
var txt =“string”;

2.3. String对象属性(常用)

(1)length - 返回字符串长度
实例:

var name = new String(“zhangDM”);
console.log(“Length =+ name.length);
执行结果:Length = 7

(2)prototype - 允许您向对象添加属性和方法
实例:

function employee(id:number,name:string) { 
    this.id = id 
    this.name = name 
 } 
 var emp = new employee(123,"admin") 
 employee.prototype.email="admin@runoob.com" // 添加属性 email
 console.log("员工号: "+emp.id) 
 console.log("员工姓名: "+emp.name) 
 console.log("员工邮箱: "+emp.email)

2.4. String方法

2.5.1. charAt()

  返回在指定位置的字符。

var str = new String("RUNOOB"); 
console.log("str.charAt(0) 为:" + str.charAt(0)); // R
console.log("str.charAt(1) 为:" + str.charAt(1)); // U 
console.log("str.charAt(2) 为:" + str.charAt(2)); // N 
console.log("str.charAt(3) 为:" + str.charAt(3)); // O 
console.log("str.charAt(4) 为:" + str.charAt(4)); // O 
console.log("str.charAt(5) 为:" + str.charAt(5)); // B

2.5.2. charCodeAt()

  返回在指定的位置的字符的 Unicode 编码。

var str = new String("RUNOOB"); 
console.log("str.charCodeAt(0) 为:" + str.charCodeAt(0)); // 82
console.log("str.charCodeAt(1) 为:" + str.charCodeAt(1)); // 85 
console.log("str.charCodeAt(2) 为:" + str.charCodeAt(2)); // 78 
console.log("str.charCodeAt(3) 为:" + str.charCodeAt(3)); // 79 
console.log("str.charCodeAt(4) 为:" + str.charCodeAt(4)); // 79
console.log("str.charCodeAt(5) 为:" + str.charCodeAt(5)); // 66

2.5.3. concat()

  连接两个或更多字符串,并返回新的字符串。

var str1 = new String( "RUNOOB" ); 
var str2 = new String( "GOOGLE" ); 
var str3 = str1.concat( str2 ); 
console.log("str1 + str2 : "+str3) // RUNOOBGOOGLE

2.5.4. indexOf()

  返回某个指定的字符串值在字符串中首次出现的位置。

var str1 = new String( "RUNOOB" ); 
var index = str1.indexOf( "OO" ); 
console.log("查找的字符串位置 :" + index );  // 3

2.5.5. lastIndexOf()

  从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。

var str1 = new String( "This is string one and again string" ); 
var index = str1.lastIndexOf( "string" );
console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 29 
index = str1.lastIndexOf( "one" ); 
console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 15

2.5.6. localeCompare()

  用本地特定的顺序来比较两个字符串。

var str1 = new String( "This is beautiful string" );
var index = str1.localeCompare( "This is beautiful string");  
console.log("localeCompare first :" + index );  // 0

2.5.7. match()

  查找找到一个或多个正则表达式的匹配。

var str="The rain in SPAIN stays mainly in the plain";
 var n=str.match(/ain/g);  // ain,ain,ain

2.5.8.replace()

  替换与正则表达式匹配的子串

var re = /(\w+)\s(\w+)/; 
var str = "zara ali"; 
var newstr = str.replace(re, "$2, $1"); 
console.log(newstr); // ali, zara

2.5.9.search()

  检索与正则表达式相匹配的值。

var re = /apples/gi; 
var str = "Apples are round, and apples are juicy.";
if (str.search(re) == -1 ) { 
	console.log("Does not contain Apples" ); 
} else { 
	console.log("Contains Apples" );
 } 

2.5.10. slice()

  提取字符串的片断,并在新的字符串中返回被提取的部分。

2.5.11. split()

  把字符串分割为子字符串数组。

var str = "Apples are round, and apples are juicy.";
 var splitted = str.split(" ", 3); 
console.log(splitted)  // [ 'Apples', 'are', 'round,' ]

2.5.12. substr()

  从起始索引号提取字符串中指定数目的字符。

2.5.13. substring()

  提取字符串中两个指定的索引号之间的字符。

var str = "RUNOOB GOOGLE TAOBAO FACEBOOK"; 
console.log("(1,2): "    + str.substring(1,2));   // U
console.log("(0,10): "   + str.substring(0, 10)); // RUNOOB GOO
console.log("(5): "      + str.substring(5));     // B GOOGLE TAOBAO FACEBOOK

2.5.14. toLocaleLowerCase()

  根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射。

var str = "Runoob Google"; 
console.log(str.toLocaleLowerCase( ));  // runoob google

2.5.15. toLocaleUpperCase()

  据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射。

var str = "Runoob Google"; 
console.log(str.toLocaleUpperCase( ));  // RUNOOB GOOGLE

2.5.16. toLowerCase()

  
把字符串转换为小写。

var str = "Runoob Google"; 
console.log(str.toLowerCase( ));  // runoob google

2.5.17. toString()

  返回字符串。

var str = "Runoob"; 
console.log(str.toString( )); // Runoob

2.5.18. ttoUpperCase()

  把字符串转换为大写。

var str = "Runoob Google"; 
console.log(str.toUpperCase( ));  // RUNOOB GOOGLE

2.5.19. valueOf()

  返回指定字符串对象的原始值。

var str = new String("Runoob"); 
console.log(str.valueOf( ));  // Runoob
  • 23
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值