格式化字符串,清除字符串中的特定字符?

问题描述:

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

根据我的理解你的问题。这可能适合你。

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; 
} 

您可以在这里使用LastIndexOf方法。

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

您可以分割,并获得最后的值

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