在最后一次斜杠vb.net之前修剪一切

时间:2016-05-04 13:15:09

标签: vb.net indexof

昨天我学会了使用流动代码

修剪文件名
    Dim NEWPATH As String = (inventorApp.ActiveDocument.FullFileName)
    NEWPATH = NEWPATH.Substring(0, NEWPATH.fIndexOf("\"c))

这真的很整洁,因为它是一种更可靠/稳定的方式,我之前使用的...哈哈今天虽然我想反过来我想在最后一次斜线之前修剪一切我怎么能这样做?

也只是出于好奇,小写字母c为(“\”c),即没有它,代码也可以正常工作?

3 个答案:

答案 0 :(得分:5)

您可以使用Path class的方法,而不是摆弄子字符串:

Dim fullpath as String = inventorApp.ActiveDocument.FullFileName

'What you're after now - the filename
Dim justTheFileName as String = Path.GetFileName(fullpath)

'a replacement for what you're already doing to get the folder name
Dim justTheFolderName as String = Path.GetDirectoryName(fullpath)

c中的小写"\"c表示您需要Char而不是String,这是IndexOf c的{​​{3}} ,但也有一个this particular overload,所以没有 { xtype:'form', items:[ { id:'uploadfile', reference:'uploadfile', xtype:'filefield', fieldLabel:'Choose file', buttonText:'Browse' }, { xtype:'button', text:'Upload', width:'10%', margin:'0 500 0 500', handler: function() { console.log("Upload clicked"); var form = this.up('form').getForm(); if(form.isValid()){ form.submit({ url: '/file-upload/', params: { csrfmiddlewaretoken: Ext.util.Cookies.get('csrftoken'); }, waitMsg: 'Uploading your file ...', success: function(fp, o) { alert('Success'); }, failure: function(form, action) { alert('Failed'); } }); } } } 它也能同样有效。

答案 1 :(得分:0)

您可以使用split()函数或此

Dim mystr As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"
Dim x As Integer = InStr(mystr, cut_at)

Dim string_before As String = mystr.Substring(0, x - 2)
Dim string_after As String = mystr.Substring(x + cut_at.Length-1)

答案 2 :(得分:0)

解决方案是使用LastIndexOf()创建一个子字符串,该子字符串仅包含最后一次slashe之后的内容:

Dim fullpath As String = inventorApp.ActiveDocument.FullFileName

Dim FileName As String = fullPath.Substring(fullPath.LastIndexOf("\"))

我喜欢这个,因为它更通用(即您可以通过"\"替换"."来恢复文件扩展名。但是,如果你确定你正在使用pathes文件,那么更喜欢James Thorpe的解决方案,使用System.IO.Path