如何在两个字符串之间添加'/'?

时间:2016-05-31 10:12:54

标签: c#

我有以下代码:

line-height: 0;

在两个字符串之间添加if (!string.IsNullOrEmpty(_location.FolderName)) { name = _location.FolderName + '/' + name; } 是否正确?或者我应该使用以下代码:

/

3 个答案:

答案 0 :(得分:13)

对于连接文件路径,您应该使用int i void frm_disposed(object sender, EventArgs e) { showFrm(ObjectsList[++i]; } 方法。

class Record: NSManagedObject
{
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?)
{
    super.init(entity: entity, insertIntoManagedObjectContext: context)
    let randomBit = String(arc4random_uniform(100))
    iD = NSDate().toIDTimeDateString() + "-" + randomBit + "OtR"

    believers = 0
    deviceTime = NSDate()
    doubters = 0
    eventTimeStarted = NSDate()
    eventTimeEnded = NSDate()
    featureImageIndex = 0
    coreLocationUsed = 0
    photoTaken = 0
    tableSection = "My Records"
    timeRecorded = deviceTime
    validationScore = 0
}
}

有一点需要注意,正如Anton在下面的评论中提到的那样,你必须确保路径中的字符有效,你可以在documentation中找到更多信息。

答案 1 :(得分:3)

为此目的使用Path.DirectorySeparatorCharhttps://msdn.microsoft.com/ru-ru/library/system.io.path.pathseparator%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Console.WriteLine("Path.AltDirectorySeparatorChar={0}", Path.AltDirectorySeparatorChar);
Console.WriteLine("Path.DirectorySeparatorChar={0}", Path.DirectorySeparatorChar);
Console.WriteLine("Path.PathSeparator={0}", Path.PathSeparator);
Console.WriteLine("Path.VolumeSeparatorChar={0}", Path.VolumeSeparatorChar);
Console.Write("Path.GetInvalidPathChars()=");
foreach (char c in Path.GetInvalidPathChars())
    Console.Write(c);
Console.WriteLine();

会给出结果:

// Path.AltDirectorySeparatorChar=/
// Path.DirectorySeparatorChar=\
// Path.PathSeparator=;
// Path.VolumeSeparatorChar=:

答案 2 :(得分:1)

使用双反斜杠会有帮助

if (!string.IsNullOrEmpty(_location.FolderName))
{
    name = _location.FolderName + "\\" + name;
}