Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How javascript strings handle classes

Shulou Source: shulou.com Published: 2022-05-31 11:02:03 09月21日 Update

This article mainly introduces the relevant knowledge of "how to deal with classes with javascript strings". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to deal with classes with javascript strings" can help you solve the problem.

/ / =

/ / function: remove the spaces on both sides of the string

/ / return: true-contains this illegal character false-does not contain

Function TrimString (str)

{

Var i,j

If (str = "") return ""

For (iTuno _ str.length) return ""

For (jroomstr.roomthmurl _ 1 ~ j > = 0 _ Ted _ jmuri -)

If (str.charAt (j)! ='') break

Return str.substring (iMagne jungle 1)

}

/ / =

/ / function: check whether it exists "

< >

Special characters such as "'& /; |"

/ / return: true-contains this illegal character false-does not contain

Function CheckSpecialChar (strSource)

{

Var intIndex =-1; / / this character was not found, return-1

Var regExpInfo = / & /

IntIndex = strSource.search (regExpInfo)

If (intIndex =-1)

{

RegExpInfo = / intIndex = strSource.search (regExpInfo)

}

If (intIndex =-1)

{

RegExpInfo = / > /

IntIndex = strSource.search (regExpInfo)

}

If (intIndex =-1)

{

RegExpInfo = / "/

IntIndex = strSource.search (regExpInfo)

}

If (intIndex =-1)

{

RegExpInfo = /'/

IntIndex = strSource.search (regExpInfo)

}

If (intIndex =-1)

{

RegExpInfo = /; /

IntIndex = strSource.search (regExpInfo)

}

/ / if (intIndex = =-1)

/ / {

/ / regExpInfo = / | /

/ / intIndex = strSource.search (regExpInfo)

/ /}

If (intIndex =-1)

{

RegExpInfo = /

IntIndex = strSource.search (regExpInfo)

}

If (intIndex =-1)

{

RegExpInfo = / /

IntIndex = strSource.search (regExpInfo)

}

If (intIndex =-1)

{

Return false

}

Else

{

Return true

}

}

/ / =

/ / function: using regular expressions, in a string, for special characters:'"

< >

& encode

/ / Parameter: strSource-the source string to be replaced

/ / return: encoded string

Function EncodeSpecialChar (strSource)

{

Var stEncodeResult = strSource

/ / empty string

If (stEncodeResult = "")

{

Return stEncodeResult

}

/ / replace the "&" character in the string with "&"

/ / when replacing, you must first replace the "&" character, otherwise, "will be replaced with". "

RegExpInfo= / > / g

StEncodeResult = stEncodeResult.replace (regExpInfo, ")

/ / replace% with "%"

RegExpInfo= /% / g

StEncodeResult = stEncodeResult.replace (regExpInfo, "%")

Return stEncodeResult

}

/ / =

/ / function: remove the spaces before and after the string

/ / Parameter: source string

Function RemoveBrank (strSource)

{

Var strArm = ""

If (strSource.length

< 1) return strArm; strArm = RemoveForwardBrank(strSource); //去掉字符串前面所有的空格 strArm = RemoveBehindBrank(strArm); //去掉字符串后面所有的空格 return strArm; } //======================================================================== //功能:去掉字符串前面所有的空格 //参数:源字符串 //返回:去掉源字符串前面空格后的字符串 function RemoveForwardBrank(strSource) { var strArm = ""; for(var i = 0;i < strSource.length;i ++) { if(strSource.charAt(i) != " ") { strArm = strSource.substring(i,strSource.length); break; } } return strArm; } //======================================================================== //功能:去掉字符串后面所有的空格 //参数:源字符串 //返回:去掉源字符串后面空格后的字符串 function RemoveBehindBrank(strSource) { var strArm = ""; var intLength = strSource.length; var intCount = 0; while(intLength --) { if(strSource.charAt(intLength) == " ") intCount ++; else break; } strArm = strSource.substring(0,strSource.length - intCount); return strArm; } //======================================================================== //功能:判断一个数是否为正整数 //参数:strNum ---- 需要判断的字符串 //返回:true ---- 整数 // false ---- 非整数 function IsIntNum(strNum) { var strCheckNum = strNum + ""; if(strCheckNum.length < 1) //空字符串 return false; else if(isNaN(strCheckNum)) //不是数值 return false; else if(parseInt(strCheckNum) < 1) //不是正数 return false; else if(parseFloat(strCheckNum) >

ParseInt (strCheckNum)) / / is not an integer

Return false

Return true

}

/ / =

/ / function: judge whether a number is positive or not

/ / Parameter: strNum-the string to be judged

/ / return: true-integer

/ / false-non-integer

Function IsTrueNum (strNum)

{

Var strCheckNum = strNum + ""

If (strCheckNum.length < 1) / / empty string

Return false

Else if (isNaN (strCheckNum)) / / is not a numerical value

Return false

Else if (parseInt (strCheckNum) < 1) / / is not a positive number

Return false

Return true

}

This is the end of the introduction to "how javascript strings deal with classes". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Characters strings functions spaces parameters integers processing positive numbers knowledge empty characters encoding special number numeric regular industry expressions different practical global Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Apple Xiaomi Microsoft