Swift字符串的插入、删除和替换-备

对应可变字符串可以插入、删除和替换,String提供了几个方法可以帮助实现这些操作。这些方法如下:

splice(_:atIndex:)。在索引位置插入字符串。

insert(_:atIndex:)。在索引位置插入字符。

removeAtIndex(_:)。在索引位置删除字符。

removeRange(_:)。删除指定范围内的字符串。

replaceRange(_:,with: String) 。使用字符串或字符替换指定范围内的字符串。

代码:

var str ="Swift"

print("原始字符串:\(str)")

str.splice("Objective-Cand ".characters, atIndex: str.startIndex)

print("插入字符串后:\(str)")

str.insert(".",atIndex: str.endIndex)

print("插入.字符后:\(str)")

str.removeAtIndex(str.endIndex.predecessor())

print("删除.字符后:\(str)")

var startIndex =str.startIndex

var endIndex =advance(startIndex, 9)

var range =startIndex...endIndex

str.removeRange(range)

print("删除范围后:\(str)")

startIndex =str.startIndex

endIndex =advance(startIndex, 0)

range =startIndex...endIndex

str.replaceRange(range,with: "C++")

print("替换范围后:\(str)")

输出结果:

原始字符串:Swift

插入字符串后:Objective-C and Swift

插入.字符后:Objective-Cand Swift.

删除.字符后:Objective-Cand Swift

删除范围后:C and Swift

替换范围后:C++ and Swift

时间: 2024-08-03 08:49:04

Swift字符串的插入、删除和替换-备的相关文章

JavaScript之jQuery-3 jQuery操作DOM(查询、样式操作、遍历节点、创建插入删除、替换、复制)

一.jQuery操作DOM - 查询 html操作 - html(): 读取或修改节点的HTML内容,类似于JavaScript中的innerHTML属性 文本操作 - text(): 读取或修改节点的文本内容,类似于JavaScript中的textContent属性 值操作 - val(): 读取或修改节点的value属性值,类似于 JavaScript 中的value值 属性操作 - attr(): 读取或者修改节点的属性 - removeAttr(): 删除节点的属性 二.jQuery操作

ios 字符串的插入删除查找与替换,删除空格

NSString *str1 = @"This is a example.";  NSMutableString *mstr = [[NSMutableString alloc] init]; //创建可变字符串  NSRange substr; //子字符串的范围mstr = [NSMutableString stringWithString:str1];  //插入字符  [mstr insertString:@"very easy " atIndex:10];

《从零开始学Swift》学习笔记(Day 14)——字符串的插入、删除和替换

原创文章,欢迎转载.转载请注明:关东升的博客 对应可变字符串可以插入.删除和替换,String提供了几个方法可以帮助实现这些操作.这些方法如下: splice(_:atIndex:).在索引位置插入字符串. insert(_:atIndex:).在索引位置插入字符. removeAtIndex(_:).在索引位置删除字符. removeRange(_:).删除指定范围内的字符串. replaceRange(_:,with: String) .使用字符串或字符替换指定范围内的字符串. 代码: va

Swift 2.0学习笔记(Day 14)——字符串的插入、删除和替换

原创文章,欢迎转载.转载请注明:关东升的博客 对应可变字符串可以插入.删除和替换,String提供了几个方法可以帮助实现这些操作.这些方法如下: splice(_:atIndex:).在索引位置插入字符串. insert(_:atIndex:).在索引位置插入字符. removeAtIndex(_:).在索引位置删除字符. removeRange(_:).删除指定范围内的字符串. replaceRange(_:, with: String) .使用字符串或字符替换指定范围内的字符串. 代码: v

C++中对字符串进行插入、替换、删除操作

#include <iostream> #include <string> using std::cout; using std::endl; using std::string; int main(void){ string str1="We can insert a string"; string str2="a str into "; //在字符串指定位置前面插入指定字符串 cout <<str1.insert(14,str

C++(十)— 字符串进行插入、替换、删除操作

 1.C++中对字符串进行插入.替换.删除操作 #include<iostream> #include<algorithm> #include<stdio.h> #include <vector> #include<string> using namespace std; int main() { string s = "123456"; // 在字符串指定位置前插入字符, cout << s.insert(2,

jQuery插入,复制、替换和删除节点

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>jQuer

JavaScript之DOM-5 增加、删除和替换节点(创建节点、插入节点、删除和替换节点)

一.创建节点 创建元素 - 使用如下方法可以创建一个新的元素节点: - document.createElement('元素名'); 创建文本节点 - 使用如下方法可以创建一个新的元素节点: - document.createTextNode('text'); 创建注释 - 使用如下方法可以创建一个HTML注释节点: - document.createComment('comment'); 创建文档片段 - 使用如下方法可以创建一个文档片段: - document.createDocumentF

Swift字符串和字符

String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 Character 类型值的集合. Swift 的 String 和 Character 类型提供了一个快速的,兼容 Unicode 的方式来处理代码中的文本信息.创建和操作字符串的语法与 C的操作方式相似,轻量并且易读.字符串连接操作只需要简单地通过 + 号将两个字符串相连即可.与 Swift 中其他值一