jQuery String Functions
Earlier I had posted about jQuery solution to split string, replace string, substring and trim string And In this post, see all jQuery/JavaScript string functions in action with examples.
Related Post:
- charAt(n): Returns the character at the specified index in a string. The index starts from 0.
1varstr ="JQUERY By Example";2varn = str.charAt(2)34//Output will be "U" - charCodeAt(n): Returns the Unicode of the character at the specified index in a string. The index starts from 0.
1varstr ="HELLO WORLD";2varn = str.charCodeAt(0);34//Output will be "72" - concat(string1, string2, .., stringX): The concat() method is used to join two or more strings. This method does not change the existing strings, but returns a new string containing the text of the joined strings.
1varstr1 ="jQuery ";2varstr2 ="By Example!";3varn = str1.concat(str2);45//Output will be "jQuery By Example!" - fromCharCode(n1, n2, ..., nX): Converts Unicode values into characters. This is a static method of the String object, and the syntax is always String.fromCharCode().
1varn = String.fromCharCode(65);23//Output will be "A" - indexOf(searchvalue, [start]): Returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs. This method is case sensitive!
1varstr="Hello world, welcome to the my blog.";2varn=str.indexOf("welcome");34//Output will be "13" - lastIndexOf(searchvalue, [start]): Returns the position of the last occurrence of a specified value in a string. The string is searched from the end to the beginning, but returns the index starting at the beginning, at postion 0. Returns -1 if the value to search for never occurs. This method is case sensitive!
1varstr="Hello planet earth, you are a great planet.";2varn=str.lastIndexOf("planet");34//Output will be "36" - substr(start, [length]): The substr() method extracts parts of a string, beginning at the character at the specified posistion, and returns the specified number of characters.
1varstr="Hello world!";2varn=str.substr(2,3)34//Output will be "llo" - substring(from, [to]): The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string. This method extracts the characters in a string between "from" and "to", not including "to" itself.
1varstr="Hello world!";2varn=str.substring(2,3)34//Output will be "l" - toLowerCase(): The toLowerCase() method converts a string to lowercase letters.
1varstr="HELLO WoRld!";2str = str.toLowerCase();3//Output will be "hello world!" - toUpperCase(): The toUpperCase() method converts a string to uppercase letters.
1varstr="hello WoRLd!";2str = str.toUpperCase();3//Output will be "HELLO WORLD!"Also read "jQuery Code: Change text to Uppercase"
Download plugin for Uppercase, lowercase, title case & pascal case - match(regexp): The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
1varstr="The rain in SPAIN stays mainly in the plain";2varn=str.match(/ain/g);34//Output will be "ain,ain,ain"5//There are 3 matches with the "ain" regex in small letters. So it returns ain 3 times. - replace(searchvalue, newvalue): The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
1varstr="Visit jQuery Blog!";2varn = str.replace("jQuery ","jQuery By Example ");34//Output will be "Visit jQuery By Example Blog!" - search(searchvalue): The search() method searches a string for a specified value, or regular expression, and returns the position of the match. This method returns -1 if no match is found.
1varstr="Visit jQuery Blog!";2varn = str.search("jQuery");34//Output will be "6" - slice(start, [end]): The slice() method extract parts of a string and returns the extracted parts in a new string. Use the start and end parameters to specify the part of the string you want to extract. The first character has the position 0, the second has position 1, and so on.
1varstr="Visit jQuery Blog!";2varn = str.slice(6,12);34//Output will be "jQuery" - split(separator, [limit]): Read Split function in jQuery
Feel free to contact me for any help related to jQuery, I will gladly help you.
在javascript、Jquery里面好像是没有String.format();这个函数的,所以我们在拼接字符串的时候就特别的辛苦,生怕又打错,而且又乱,所以就自己去写一个函数来代替。
String.format = function() { if (arguments.length == 0) return null; var str = arguments[0]; for ( var i = 1; i < arguments.length; i++) { var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm'); str = str.replace(re, arguments[i]); } return str; }; // var a = "我喜欢吃{0},也喜欢吃{1},但是最喜欢的还是{0},偶尔再买点{2}"; // alert(String.format(a, "苹果","香蕉","香梨")); // 结果:我喜欢吃苹果,也喜欢吃香蕉,但是最喜欢的还是苹果,偶尔再买点香梨 |
是从0位开始的。
建议自己把这些东西写进一个js文件,每次使用就调用可以了
jQuery String Functions的更多相关文章
- jquery.string.js
/** * jquery.string - Prototype string functions for jQuery * version: 1.1.0 * (c) 2008-2011 David E ...
- Part 11 string functions in sql server
Built in string functions in sql server 2008 LEFT, RIGHT, CHARINDEX and SUBSTRING functions in sql s ...
- [Training Video - 4] [Groovy] String Functions
def x="I like to read books before bed" def temp = x.split(" ") log.info "S ...
- Mysql String Functions
SUBSTRING_INDEX(str,delim,count) 按标识符截取指定长度的字符串 mysql); -> 'www.mysql' mysql); -> 'mysql.com' ...
- 【python】string functions
1.str.replace(word0,word1) ##用word1替换str中所有的word0 >>> 'tea for too'.replace('too', 'two') ...
- jquery判空 string类型的日期比较大小
jquery 判空 if(value.length<=0){ alert("kongzhi"); } jquery string类型的日期比较大小 var startTim ...
- [Javascript] String Padding in Javascript using padStart and padEnd functions
ES2017 added two new string functions. They are padStart and padEndfunctions. In this lesson, we wil ...
- 一个jQuery扩展工具包
带有详尽注释的源代码: var jQuery = jQuery || {}; // TODO // ###################################string操作相关函数### ...
- JQuery常用API 核心 效果 JQueryHTML 遍历 Event事件
JQuery 常用API 参考资料:JQuery 官网 jQuery API 中文文档 核心 jQuery 对象 jQuery() 返回匹配的元素集合,无论是通过在DOM的基础上传递的参数还是创建 ...
随机推荐
- LintCode Min Stack
用两个stack, 第一个按顺序放所有值,第二个只放当前最小值. 注意: 1. 最小值有多个则都放到两个stack里, 尤其别忘放第二个: 2. pop时若两个stack的最上面值相等则都pop, 不 ...
- 使用Quartz.net动态设置定时时间问题
关于使用Quartz.net就不用解释了.. 应客户需求问题..需要将做一个界面设置定时的时间.因此我在百度一番..用CronExpression类可以设置时间... 我知道这个类有定义好的字段..不 ...
- iOS测试常见崩溃
什么是崩溃日志,从哪里能得它? iOS设备上的应用闪退时,操作系统会生成一个崩溃报告,也叫崩溃日志,保存在设备上.崩溃日志上有很多有用的信息,包括应用是什么情况下闪退的.通常,上面有每个正在执行线程的 ...
- Express4.x常用API(一):res
最近在学习NodeJS,用到了express,看着官网上的API手册,打算把其中比较常用到的API根据自己理解翻译一下,方便自己学习使用. 该篇打算用来记录下express中res. 由于水平有限,希 ...
- 笔记 线程(threads)
线程:CPU使用的基本单元(线程ID.程序计数器.寄存器集合.栈). 多线程:一个进程有多个线程 多线程的优点: 增加响应度:当一个交互程序部分阻塞,该程序能继续执行 一个应用程序在同一地址空间有多个 ...
- Linear Algebra lecture6 note
Vector spaces and subspaces Column space of A solving Ax=b Null space of A Vector space requiremen ...
- [转] "self = [super init]"的解释与潜藏bug
Objective-C的推荐init方法写法如下: - (id) init { if(self = [super init]) { //为子类增加属性进行初始化 } return self; } 这里 ...
- Objective-C关键字和概念
Objective-C关键字和概念 @ 看到这个关键字,我们就应该想到,这是Object-C对C语言的扩展,例如@interface XXX. @interface 声明类 @implementati ...
- python中的input,print
此用例在python3.3.5中测试通过: 输入:在python中输入是使用input,下面示例代码表示把输入的值存入变量s中,并输入s 在这里提醒一下:使用input获取的值都是string类型
- notepad++
plugin-manager 先要保证plugin-manager是最新的,然后才能下载其它的插件进行安装. compare插件,文件对比工具