Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将字符串添加到另一个字符串的开头_Ios_String_Swift - Fatal编程技术网

Ios 将字符串添加到另一个字符串的开头

Ios 将字符串添加到另一个字符串的开头,ios,string,swift,Ios,String,Swift,基本问题 我有两条线。我想将一个字符串添加到另一个字符串?下面是一个例子: var secondString= "is your name." var firstString = "Mike, " 这里我有两条线。我想将firstString添加到secondString,反之亦然。(应该是:firstString+=secondString) 更多细节 我有5个字符串 let first = "7898" let second = "00" let third = "5481" let fo

基本问题

我有两条线。我想将一个字符串添加到另一个字符串?下面是一个例子:

var secondString= "is your name."
var firstString = "Mike, "
这里我有两条线。我想将
firstString
添加到
secondString
,反之亦然。(应该是:
firstString+=secondString

更多细节

我有5个
字符串

let first = "7898"
let second = "00"
let third = "5481"
let fourth = "4782"

var fullString = "\(third):\(fourth)"
我确信
第三个
第四个
将在
完整字符串中
,但我不知道
第一个
第二个

所以我将做一个
if语句
检查
second
是否有
00
。如果是这样,
first
second
将不会进入
fullString
。如果没有,则
second将进入
fullString`

然后我将首先检查
是否有
00
。如果是,那么
first
将不会进入
fullString
的内部,如果不是,它将进入

问题是,我需要它们的顺序是一样的:第一,第二,第三,第四。因此,在if语句中,我需要一种方法,在
fullString
的开头添加
first
second
,如下所示:

可以使用加法运算符(+)将字符串值相加(或连接),以创建新的字符串值:

还可以使用加法赋值运算符(+=)将字符串值附加到现有字符串变量:

可以使用字符串类型的append()方法将字符值附加到字符串变量:

因此,您可以以任何方式添加这些形状或形式。 其中包括

secondstring += firststring
编辑以适应新信息: 这意味着您可以始终在位添加字符串,而无需重新创建任何对象

类似于(伪代码)


Re。你的基本问题是:

 secondString = "\(firstString)\(secondString)"
var fullString: String

if second == "00" {
    fullString = third + fourth
} else if first == "00" {
    fullString = second + third + fourth
} else {
    fullString = first + second + third + fourth
}

下面是一种在注释开头插入字符串的方法(“无需重置”)(
first
second
前面):


... 您是否尝试过secondString+=firstString?我更新了问题我可以这样做,但我想知道是否有不重置字符串的方法。请参阅我的最新评论。谢谢!我尝试了4个if语句,但仍然无法正确执行!
secondstring += firststring
if(second != "00")
{
  fullstring = second + fullstring
  //only do something with first if second != 00
  if(first != "00")
  {
   fullstring = first + fullstring
  }
}
 secondString = "\(firstString)\(secondString)"
secondString = firstString + secondString
let range = second.startIndex..<second.startIndex
second.replaceRange(range, with: first)
var fullString: String

if second == "00" {
    fullString = third + fourth
} else if first == "00" {
    fullString = second + third + fourth
} else {
    fullString = first + second + third + fourth
}