我有一个字符串"""
,我需要修剪它的开头和结尾"
并获得值"
,这样做的最佳方法是什么?
如果我仅使用mystring.Substring(1,1)
和Substring(mystring.Length-2,1 )
来获取第一个和最后一个字符,然后与"
进行比较,如果它们相等,则再次使用该字符的子字符串,那太复杂了?有什么短方法可以只修剪第一个"
和最后一个"
并保持数据值"""
?谢谢
更新:
Trim('"')
就是这种情况之一,对于用“换行的任何其他字符串,我都可以"""""
,但是如果我对它做同样的事情,它将是空的,并且我不知道哪个如果有""""""""
或"
等,我会在以后得到的模式。
简而言之,无论该字符串是什么样的,我都只需要修剪第一个"""""
和最后一个"""""""...
,但是对于DEBUG | Karaf Lock Monitor Thread | Main | - - | Waiting for the lock ...
,un 28, 2018 11:52:07 AM org.apache.karaf.main.Main launch
INFO: Installing and starting initial bundles
Jun 28, 2018 11:52:07 AM org.apache.karaf.main.Main launch
INFO: All initial bundles installed and set to start
2018-06-28T11:52:08,652 | INFO | Karaf Lock Monitor Thread | SocketLock | - - | in override lock() method
2018-06-28T11:52:08,662 | INFO | Karaf Lock Monitor Thread | SocketLock | - - | in override isAlive() method
2018-06-28T11:52:08,663 | DEBUG | Karaf Lock Monitor Thread | Main | - - | Waiting for the lock ...
2018-06-28T11:52:08,703 | DEBUG | CM Configuration Updater (Update: pid=org.ops4j.pax.url.mvn) | mvn | 4 - org.ops4j.pax.url.mvn - 2.
5.4 | ServiceEvent REGISTERED - [org.ops4j.pax.url.mvn.MavenResolver] - org.ops4j.pax.url.mvn
2018-06-28T11:52:08,706 | DEBUG | CM Configuration Updater (Update: pid=org.ops4j.pax.url.mvn) | mvn | 4 - org.ops4j.pax.url.mvn - 2.
5.4 | ServiceEvent REGISTERED - [org.osgi.service.url.URLStreamHandlerService] - org.ops4j.pax.url.mvn
2018-06-28T11:52:08,716 | DEBUG | pool-2-thread-2 | core | 10 - org.apache.karaf.features.core - 4.1.5 | ServiceEvent REGISTERED - [o
rg.osgi.framework.hooks.resolver.ResolverHookFactory] - org.apache.karaf.features.core
这样的任何字符串,我都不希望为空值。
答案 0 :(得分:0)
您应使用Regex.Replcae
,其外观应类似于:
string pattern = @"\".*\"";
string replacement = "";
string input = ""asdf""asf"";
string result = Regex.Replace(input, pattern, replacement);
答案 1 :(得分:0)
与大多数类似的问题一样,正则表达式就是答案。这是对字符串取消引号的扩展方法(意思是将“ xxx”更改为xxx,将“ yyy”更改为yyy)。它不会处理字符串中间引号的转义问题(例如,将“更改为”或“ \”更改为“),但这通常是使用不同方法进行的单独练习。这也删除了所有前导空格。
/// <summary> Remove single or double quotes around an entry if any. This also trims leading and trailing whitespace so
/// it's more like a positional parameter filter than pure unquoting. </summary>
/// <param name="target"> Target string to unquote</param>
/// <returns> Trimmed and Unquoted string</returns>
public static string UnQuote(this string target)
=> Regex.Replace(target, @"^\s*([\x22\x27])(.*)\1\s*$", "${2}");
答案 2 :(得分:0)
这样做:
class Program
{
static void Main(string[] args)
{
string str = "\"\"\"";
str = TrimValueFromString("\"", str);
}
private static string TrimValueFromString(string value, string original)
{
string trimmed = original;
if(original.IndexOf(value) == 0)
{
trimmed = original.Substring(value.Length);
}
if(trimmed.LastIndexOf(value) == (trimmed.Length - value.Length))
{
trimmed = trimmed.Substring(0, trimmed.LastIndexOf(value));
}
return trimmed;
}
}
输入:
“”“
输出:
”
使用此实现,您将能够修剪每个字符串而无需重复重复该值,还有另一个示例:
class Program
{
static void Main(string[] args)
{
string str = "asdciccioasdciccioasd";
str = TrimValueFromString("asd", str);
}
private static string TrimValueFromString(string value, string original)
{
string trimmed = original;
if(original.IndexOf(value) == 0)
{
trimmed = original.Substring(value.Length);
}
if(trimmed.LastIndexOf(value) == (trimmed.Length - value.Length))
{
trimmed = trimmed.Substring(0, trimmed.LastIndexOf(value));
}
return trimmed;
}
}
输入:
asdasdciccioasdciccioasdasd
输出:
asdciccioasdciccioasd