c 字符串数组数组,,

在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
标签:至少1个,最多5个
原文链接:
chartAt()与charCodeAt()
参数:基于0的字符位置
chartAt()以单字符字符串的形式返回给定位置的那个字符。而charCodeAt()返回的是字符编码。
var stringValue = 'hello world';
/*chartAt()*/
console.log(stringValue.chartAt(1));
字符串操作方法
concat()(数组中也有该方法)
参数:一个或多个字符串
将一个会多个字符串拼接起来,当然更常用的是使用 “+” 进行拼接
substring()与slice()(数组中也有此方法)
参数:指定子字符串的开始位置,子字符串到哪里结束
作用:创建新的子字符串(可以理解为字符串截取)
参数:指定子字符串的开始位置,返回的子字符串的字符个数
作用:创建新的子字符串(可以理解为字符串截取)
repeat()(ES6新增)
参数:数字(表示重复的次数)
作用:将原字符串重复n次
如果传入负数,则报错,传入小数和NaN等同于传入0
substring,slice,substr,repeat均返回子字符串,不会修改原来的字符串
var stringValue = "hello world";
alert(stringValue.slice(3));
//"lo world"
alert(stringValue.substring(3));
//"lo world"
alert(stringValue.substr(3));
//"lo world"
alert(stringValue.slice(3, 7));
alert(stringValue.substring(3,7));
alert(stringValue.substr(3, 7));
//"lo worl"
/*repeat()*/
var a = 'he';
var b = a.repeat(3);
console.log(`${a}---${b}`); /
//"he---hehehe"
当给这三个方法传入负值的时候,三个的表现不同:
slice()会将传入的负值与字符串的长度相加
substr()会将第一个位置的负值参数加上字符串长度后转为正数,而第二个位置的负值将转化为0
substring()会把所有的负参数转化为0
repeat()会报错
字符串位置方法
indexOf()和lastIndexOf()(数组中也有该方法)
参数:要搜索的子字符串,开始搜索的位置(可选)
搜索给定的子字符串,如果找到则返回位置,否则返回-1
var stringValue = "hello world";
alert(stringValue.indexOf("o"));
alert(stringValue.lastIndexOf("o"));
这两个方法在搜索到第一个匹配的子字符串后就停止运行,所以如果想找到字符串中所有的子字符串出现的位置,可以循环调用indexOf或lastIndexOf。
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while(pos & -1){
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
alert(positions);
//"3,24,32,35,52"
ES6新增includes()、startsWith()、endsWith()
includes():返回布尔值,表示是否找到了参数字符串
startsWith():返回布尔值,表示参数字符串是否在源字符串的头部
endsWith():返回布尔值,表示参数字符串是否在源字符串的尾部
这三个方法的参数与indexOf(),lastIndexOf()一样
var s = 'Hello world';
s.startsWith('world',6);
s.endsWith('Hello',5);
s.includes('Hello',6);
注意:使用第2个参数n时,endsWith的行为与其他两个方法有所不同。它针对前面n个字符,而其他两个方法针对从第n个位置开始直到字符串结束的字符。
去空格--trim()
ES5中新增trim()方法用于去除字符串的左右空格,该方法会创建一个字符串的副本,不会改变原有的字符串,此外,Firefox 3.5+、Safari 5+和 Chrome 8+还支持非标准的 trimLeft()和 trimRight()方法,分别用于删除字符串开头和末尾的空格。
其实去空格可以使用正则去匹配的去掉,这里写一个去空格函数
str要处理的字符串
类型:l 去除左边的空白
r去除右边空白
b去掉两边的空白
a去除所有空白*/
function trim (str,type) {
var type=type||"b";
if(type=="b"){
return str.replace(/^\s*|\s*$/g,"");
}else if(type=="l"){
return str.replace(/^\s*/g,"");
}else if(type=="r"){
return str.replace(/\s*$/g,"");
}else if(type=="a"){
return str.replace(/\s*/g,"");
字符串大小写转换
toLowerCase()、toLocaleLowerCase()、toUpperCase()和 toLocaleUpperCase()
字符串的模式匹配方法
参数:一个正则表达式或RegExp对象
返回一个数组。在字符串上调用这个方法本质上与调用RegExp的exec()方法相同。
var text = "cat, bat, sat, fat";
var pattern = /.at/;
//与 pattern.exec(text)相同
var matches = text.match(pattern);
alert(matches.index);
alert(matches[0]);
alert(pattern.lastIndex);
参数:一个正则表达式或RegExp对象
返回字符串中第一个匹配项的索引,如果没有找到,则返回-1
var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
alert(pos);
参数:一个RegExp对象或者一个字符串(这个字符串不会被转换成正则表达式),一个字符串或一个函数
利用replace()进行替换的时候,如果传入的是字符串,则只会替换第一个子字符串,要想替换所有的子字符串,则需要传入一个正则表达式,而且要指定全局(g)标志
var text = 'cat , bat , sat , fat';
var result = text.replace('at','ond');
console.log(result); // =&'cont , bat , sat , fat'
result = text.replace(/at/g,'ond');
console.log(result); //=&'cont , bont , sont , font'
该方法并不改变调用它的字符串本身,只是返回一个新的替换后的字符串。
当第二个参数为函数时函数的返回值作为替换字符串。与第二个参数是字符串一样,如果第一个参数是正则表达式,并且全局匹配,则这个函数的方法将被多次调用,每次匹配都会被调用。
该函数的参数:
match:匹配的子串
p1,p2...:假如replace()方法的第一个参数是RegExp对象,则代表第n个括号匹配的字符串。
offset:匹配到的子字符串在原字符串中的偏移量。(比如,如果原字符串是“abcd”,匹配到的子字符串时“bc”,那么这个参数是1)
被匹配的原字符串
function replacer(match , p1 , p2 , p3 , offset , string){
// p1 is nondigits, p2 digits, and p3 non-alphanumerics
console.log(`${match}
${string}`);
/* =& abc12345#$*%
abc12345#$*%"
console.log([p1, p2, p3].join(' - ')); // =& "abc - 12345 - #$*%"
return [p1, p2, p3].join(' - ');
var newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer); // =&"abc - 12345 - #$*%"
参数:用于分隔字符串的分隔符,数字(可选,用于指定数组的大小)
作用:基于指定的分隔符将一个字符串分割成多个子字符串,并将结果放在一个数组中,分隔符可以是字符串,也可以是RegExp对象
var color = 'red,blue,yellow,black';
var color1 = color.split(',');
// =&['red','blue','yellow','black']
var color2 = color.split(',',2);
// =&['red','blue']
var color3 = color.split(/[^\,]+/); // =&["", ",", ",", ",", ""]
最后一个调用split的时候,出现了前后的两个空白,是因为通过正则表达式指定的分隔符出现在了字符串的开头和结尾。
localeCompare()
这个方法用于比较两个字符串,并返回下列值中的一个:
如果字符串在字母表中应该排在字符串参数之前,则返回负数(大多情况下为-1)
如果相等,则返回0
如果排在字符串参数之前,则返回正数(大多数情况下为1)
fromCharCode()
String构造函数的一个静态方法
参数:一个或多个字符串编码
作用:将接收到的一个或多个字符串编码转换成一个字符串,这个方法与实例方法charCodeAt()执行相反的操作。
/*fromCharCode*/
String.fromCharCode(104,101,108,108,111);
// =&hello
/*charCodeAt*/
let s = 'hello';
for(let i=0;i&s.i++){
console.log(`${s[i]}----${s[i].charCodeAt()}`);
"h----104"
"e----101"
"l----108"
"l----108"
"o----111"
最后写一个字符串与数组方法应用的一个例子,熟悉它们方法的话很简单,不熟悉就会觉得有点儿乱。
let s = 'hello';
let news = s.split('').reverse().join('');
console.log(news); // =& "olleh"
另附js中String和Array方法的总结图:
1 收藏&&|&&11
你可能感兴趣的文章
7 收藏,573
1 收藏,764
原文链接挂了?
原文链接挂了?
好了,谢谢提醒
好了,谢谢提醒
最后的String和Array方法的总结图不是很清晰,可以发给我吗?
最后的String和Array方法的总结图不是很清晰,可以发给我吗?
晚上回去给你发吧
晚上回去给你发吧
楼主的第一个字符串的实例方法写错了,应该是‘charAt()’
楼主的第一个字符串的实例方法写错了,应该是‘charAt()’
分享到微博?
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。博主最新文章
博主热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)C# 字符串(String)
在 C# 中,您可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量。string 关键字是 System.String 类的别名。
您可以使用以下方法之一来创建 string 对象:
通过给 String 变量指定一个字符串
通过使用 String 类构造函数
通过使用字符串串联运算符( + )
通过检索属性或调用一个返回字符串的方法
通过格式化方法来转换一个值或对象为它的字符串表示形式
下面的实例演示了这点:
namespace StringApplication
class Program
static void Main(string[] args)
//字符串,字符串连接
string fname,
fname = "Rowan";
lname = "Atkinson";
string fullname = fname +
Console.WriteLine("Full Name: {0}", fullname);
//通过使用 string 构造函数
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//方法返回字符串
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//用于转化值的格式化方法
DateTime waiting = new DateTime(, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}",
Console.WriteLine("Message: {0}", chat);
Console.ReadKey() ;
当上面的代码被编译和执行时,它会产生下列结果:
Full Name: RowanAtkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 17:58 on Wednesday, 10 October 2012
String 类有以下两个属性:
序号属性名称 & 描述
1Chars在当前 String 对象中获取 Char 对象的指定位置。
2Length在当前的 String 对象中获取字符数。
String 类有许多方法用于 string 对象的操作。下面的表格提供了一些最常用的方法:
序号方法名称 & 描述
1public static int Compare(
string strA,
string strB
比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。该方法区分大小写。
2public static int Compare(
string strA,
string strB,
bool ignoreCase
比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。但是,如果布尔参数为真时,该方法不区分大小写。
3public static string Concat(
string str0,
string str1
连接两个 string 对象。
4public static string Concat(
string str0,
string str1,
string str2
连接三个 string 对象。
5public static string Concat(
string str0,
string str1,
string str2,
string str3
连接四个 string 对象。
6public bool Contains(
string value
返回一个表示指定 string 对象是否出现在字符串中的值。
7public static string Copy(
string str
创建一个与指定字符串具有相同值的新的 String 对象。
8public void CopyTo(
int sourceIndex,
char[] destination,
int destinationIndex,
从 string 对象的指定位置开始复制指定数量的字符到 Unicode 字符数组中的指定位置。
9public bool EndsWith(
string value
判断 string 对象的结尾是否匹配指定的字符串。
10public bool Equals(
string value
判断当前的 string 对象是否与指定的 string 对象具有相同的值。
11public static bool Equals(
判断两个指定的 string 对象是否具有相同的值。
12public static string Format(
string format,
Object arg0
把指定字符串中一个或多个格式项替换为指定对象的字符串表示形式。
13public int IndexOf(
char value
返回指定 Unicode 字符在当前字符串中第一次出现的索引,索引从 0 开始。
14public int IndexOf(
string value
返回指定字符串在该实例中第一次出现的索引,索引从 0 开始。
15public int IndexOf(
char value,
int startIndex
返回指定 Unicode 字符从该字符串中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。
16public int IndexOf(
string value,
int startIndex
返回指定字符串从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。
17public int IndexOfAny(
char[] anyOf
返回某一个指定的 Unicode 字符数组中任意字符在该实例中第一次出现的索引,索引从 0 开始。
18public int IndexOfAny(
char[] anyOf,
int startIndex
返回某一个指定的 Unicode 字符数组中任意字符从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。
19public string Insert(
int startIndex,
string value
返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。
20public static bool IsNullOrEmpty(
string value
指示指定的字符串是否为 null 或者是否为一个空的字符串。
21public static string Join(
string separator,
string[] value
连接一个字符串数组中的所有元素,使用指定的分隔符分隔每个元素。
22public static string Join(
string separator,
string[] value,
int startIndex,
连接接一个字符串数组中的指定位置开始的指定元素,使用指定的分隔符分隔每个元素。
23public int LastIndexOf(
char value
返回指定 Unicode 字符在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。
24public int LastIndexOf(
string value
返回指定字符串在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。
25public string Remove(
int startIndex
移除当前实例中的所有字符,从指定位置开始,一直到最后一个位置为止,并返回字符串。
26public string Remove(
int startIndex,
从当前字符串的指定位置开始移除指定数量的字符,并返回字符串。
27public string Replace(
char oldChar,
char newChar
把当前 string 对象中,所有指定的 Unicode 字符替换为另一个指定的 Unicode 字符,并返回新的字符串。
28public string Replace(
string oldValue,
string newValue
把当前 string 对象中,所有指定的字符串替换为另一个指定的字符串,并返回新的字符串。
29public string[] Split(
params char[] separator
返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。
30public string[] Split(
char[] separator,
返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。int 参数指定要返回的子字符串的最大数目。
31public bool StartsWith(
string value
判断字符串实例的开头是否匹配指定的字符串。
32public char[] ToCharArray()返回一个带有当前 string 对象中所有字符的 Unicode 字符数组。
33public char[] ToCharArray(
int startIndex,
int length
返回一个带有当前 string 对象中所有字符的 Unicode 字符数组,从指定的索引开始,直到指定的长度为止。
34public string ToLower()把字符串转换为小写并返回。
35public string ToUpper()把字符串转换为大写并返回。
36public string Trim()移除当前 String 对象中的所有前导空白字符和后置空白字符。
上面的方法列表并不详尽,请访问 MSDN 库,查看完整的方法列表和 String 类构造函数。
下面的实例演示了上面提到的一些方法:
比较字符串
namespace StringApplication
class StringProg
static void Main(string[] args)
string str1 = "This is test";
string str2 = "This is text";
if (String.Compare(str1, str2) == 0)
Console.WriteLine(str1 + " and " + str2 +
" are equal.");
Console.WriteLine(str1 + " and " + str2 + " are not equal.");
Console.ReadKey() ;
当上面的代码被编译和执行时,它会产生下列结果:
This is test and This is text are not equal.
字符串包含字符串:
namespace StringApplication
class StringProg
static void Main(string[] args)
string str = "This is test";
if (str.Contains("test"))
Console.WriteLine("The sequence 'test' was found.");
Console.ReadKey() ;
当上面的代码被编译和执行时,它会产生下列结果:
The sequence 'test' was found.
获取子字符串:
namespace StringApplication
&&&&class StringProg
&&&&&&&&static void Main(string[] args)
&&&&&&&&&&&&string str = "Last night I dreamt of San Pedro";
&&&&&&&&&&&&Console.WriteLine(str);
&&&&&&&&&&&&string substr = str.Substring(23);
&&&&&&&&&&&&Console.WriteLine(substr);
&&&&&&&&&&&&Console.ReadKey() ;
当上面的代码被编译和执行时,它会产生下列结果:
Last night I dreamt of San Pedro
连接字符串:
namespace StringApplication
class StringProg
static void Main(string[] args)
string[] starray = new string[]{"Down the way nights are dark",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"};
string str = String.Join("\n", starray);
Console.WriteLine(str);
Console.ReadKey() ;
当上面的代码被编译和执行时,它会产生下列结果:
Down the way nights are dark
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop
2周前 (04-03)
感谢您的支持,我会继续努力的!
扫码打赏,你说多少就多少
记住登录状态
重复输入密码【图文】字符串的有关算法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
字符串的有关算法
&&C语言中关于字符串有关算法的教学
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢6168人阅读
要说C语言中最让我觉得棘手的,就是字符数组的,C语言中没有字符串这个类型,字符串只能存放在字符型数组中。
那么,我们先来看看如何给一个一维数组赋值:
1、定义的时候直接赋值:
char arr[10] = {"kitty"};
这种写法也可以直接省略花括号,直接写成:
char arr[10] = "kitty";
2、初始化列表,把各个字符依次赋给数组中的元素:
char arr[10] = {'k','i','t','t','y'}
这种写法是不可以省略花括号的。
3、利用字符串处理函数
strcpy( str1, str2)可以将字符串2的内容复制到字符串1中。
char arr[10];
strcpy(arr, "kitty");
利用字符串处理函数对字符串进行赋值,要注意(1)字符数组1必须定义的足够大,以便容纳字符串2;(2)需要包含头文件string.h
对一维数组进行赋值时需要注意的易错情况:
char arr[10];
arr[10] = "kitty";
数组元素的下标是从序号0开始,arr[10]最大的下标值为9,况且arr[10]指的是下标为10的元素,一个字符是无法容纳一个字符串的。
char arr[10];
arr = "kitty";
arr虽然是个指针,但是它已经指向了堆栈中的10个字符空间,无法指向”kitty“这个常量了。

我要回帖

更多关于 字符数组 的文章

 

随机推荐