2015-03-02 62 views
-9
string FormatString(string text) 
{ 

// example of a string text recieved... 
// /test/test1/test2/important-text-here-random 

// example a-top => return should be... 
// Important Text Here Random 

// please and ty 

// a quirk I have is that the text that needs to be formatted is always after that last/incase I do not know how to calculate a random amount of/in a string. 

} 

問題出在功能本身。格式化字符串,清除字符串中的特定字符?

請幫忙?我需要最有效的方式這樣做......收到將文本

例如:/測試/ TEST1/TEST2 /重要的文本,這裏隨機

我需要它格式化爲:重要的文本這裏隨機

謝謝。

+1

我不能讓你的問題。你可以請嘗試重新措辭嗎? – Sachin 2015-03-02 06:07:22

+0

我重新措辭它。 – Andrew 2015-03-02 06:08:59

+0

如果這是一個文件路徑,請嘗試使用'var afterSlash = Path.GetFileName(text);'。 – 2015-03-02 06:11:49

回答

2

根據我的理解你的問題。這可能適合你。

string FormatString(string text) 
{ 
    // Get the last string and replace the "-" to space. 
    string output = text.split('/').Last().Replace("-"," "); 

    // convert it into title case 
    output = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(output); 
    return output; 
} 
0

您可以在這裏使用LastIndexOf方法。

string FormatString(string text) 
{  
    return text.Remove(0,text.LastIndexOf("/")+1);  
} 
0

您可以分割,並獲得最後的值

string FormatString(string text) 
{ 
string [] allvalues=text.Split('/'); 
return (allvalues[allvalues.Length - 1]).Replace("-"," "); 
}