从List
格式化字符串时遇到一些问题这是列表值的图片:
现在我设法操纵了一些值,但其他人没有,这是我曾经操纵过的:
string prepareStr(string itemToPrepare) {
string first = string.Empty;
string second = string.Empty;
if (itemToPrepare.Contains("\"")) {
first = itemToPrepare.Replace("\"", "");
}
if (first.Contains("-")) {
int beginIndex = first.IndexOf("-");
second = first.Remove(beginIndex, first.Length - beginIndex);
}
return second;
}
这是结果的图片:
我需要在没有(-startup,-minimzed,MSRun,双撇号)的情况下获得清晰的Path。
希望有人能在这里看到我做错了什么。
编辑我更新的代码:
void getStartUpEntries() {
var startEntries = StartUp.getStartUp();
if (startEntries != null && startEntries.Count != 0) {
for (int i = 0; i < startEntries.Count; i++) {
var splitEntry = startEntries[i].Split(new string[] { "||" }, StringSplitOptions.None);
var str = splitEntry[1];
var match = Regex.Match(str, @"\|\|""(?<path>(?:\""|[^""])*)""");
var finishedPath = match.Groups["path"].ToString();
if (!string.IsNullOrEmpty(finishedPath)) {
if (File.Exists(finishedPath) || Directory.Exists(finishedPath)) {
var _startUpObj = new StartUp(splitEntry[0], finishedPath,
"Aktiviert: ", new Uri("/Images/inWatch.avOK.png", UriKind.RelativeOrAbsolute),
StartUp.getIcon(finishedPath));
_startUpList.Add(_startUpObj);
}
else {
var _startUpObjNo = new StartUp(splitEntry[0], finishedPath,
"Aktiviert: ", new Uri("/Images/inWatch.avOK.png", UriKind.RelativeOrAbsolute),
StartUp.getIcon(string.Empty));
_startUpList.Add(_startUpObjNo);
}
}
var _startUpObjLast = new StartUp(splitEntry[0], splitEntry[1],
"Aktiviert: ", new Uri("/Images/inWatch.avOK.png", UriKind.RelativeOrAbsolute),
StartUp.getIcon(string.Empty));
_startUpList.Add(_startUpObjLast);
}
lstStartUp.ItemsSource = _startUpList.OrderBy(item => item.Name).ToList();
}
答案 0 :(得分:1)
您可以使用正则表达式来提取路径:
var str = @"0Raptr||""C:\Program Files (x86)\Raptr\raptrstub.exe"" --startup"
var match = Regex.Match(str, @"\|\|""(?<path>(?:\""|[^""])*)""");
Console.WriteLine(match.Groups["path"]);
这将匹配两个引号字符之间的任何(甚至为空)文本(转义引号或任何不是引号的字符),前面有两个管道符号。
答案 1 :(得分:0)
同样,您可以简单地拆分双引号,因为我看到您的示例中出现了重复,并在分割数组中取出第二项:
int main(){
/*main method*/
char original[10];
char c;
int i;
for(i=0; (c=getchar())!=EOF;i++){
original[i]=c;
}
printf(totalElements(original));
}
int totalElements(char a[]){
char c;
int i=0;
while((c=a[i])!='\0'){
i++;
}
return i;
}
答案 2 :(得分:0)
这是对逻辑的更新,而不使用任何正则表达式:
private string prepareStr(string itemToPrepare)
{
string result = null;
string startString = @"\""";
string endString = @"\""";
int startPoint = itemToPrepare.IndexOf(startString);
if (startPoint >= 0)
{
startPoint = startPoint + startString.Length;
int EndPoint = itemToPrepare.IndexOf(endString, startPoint);
if (EndPoint >= 0)
{
result = itemToPrepare.Substring(startPoint, EndPoint - startPoint);
}
}
return result;
}