某些字符选择字符,直到在C#中的某些字符
好了,所以我的代码看起来有点像这样某些字符选择字符,直到在C#中的某些字符
// Returns the note of a favorite pown if it exists
string GetFavoriteNote(int id)
{
string notelist = Properties.Settings.Default.FavoriteNotesList;
// If there's a note, return it
if (notelist.Contains("'" + id + ":"))
{
// What to do here?
}
// If there's no note, return an empty string
else
{
return String.Empty;
}
}
现在它基本上是一个系统,其中每个ID用户可以设置一个音符,它将被保存在这种格式:'id:note','id:note'
,
现在我要做的就是选择一个音符不知何故并返回,所以我不得不从"'" + id + ":"
喜欢选择直到“
如果有人知道如何做到这一点,请帮帮我。 感谢
使用正则表达式似乎是最干净的方法对我说:
string regexFormat = "'{0}:(.*?)'";
Match match = Regex.Match(notelist, string.Format(regexFormat, id));
return match.Success ? match.Groups[1].Value : string.Empty;
或者然而,你可以使用字符串分割:
var notes = notelist.Split(',');
var idString = "'" + id + ":";
var note = notes.FirstOrDefault(n => n.StartsWith(idString));
if (note == null) return string.Empty;
return note.Substring(idString.Length, note.Length - (idString.Length + 1));
我不知道你做了什么,但它的工作,谢谢^^ – user1071461 2012-03-08 09:29:02
@ user1071461:我会_strongly_建议不要使用你不明白的代码。 – 2012-03-08 09:55:46
notelist.Substring(notelist.IndexOf("'" + id + ":"), (notelist.IndexOf("'") - notelist.IndexOf("'" + id + ":")));
这应该做的伎俩,你可以选择文本由子到一个新的字符串。 substring(startindex,lenght);
ID会为每个音符更改,并且可以有多个,这个工作怎么样? – 2012-03-08 09:21:07
它没有工作,但无论如何谢谢 – user1071461 2012-03-08 09:29:26
我的代码示例将返回一个字符串,DIT你放在另一个字符串 – middelpat 2012-03-08 09:32:41
尝试
int StartIndex = notelist.IndexOf("'" + id.ToString() + ":");
string result = string.Empty;
if (StartIndex >= 0)
{
string tempstr = notelist.SubString (StartIndex + ("'" + id.ToString() + ":").Length);
result = tempstr.SubString (0, tempstr.IndexOf ("'"));
}
return result;
据我了解你的代码,下面的代码会给你一个解决方案
string IdWithNote = string.Empty;
string noteList = Properties.Settings.Default.FavoriteNotesList;//your string type note list
List<string> listNote = new List<string>();//newly created string type collection
listNote=noteList.Split(',').ToList<string>();
int index=listNote.IndexOf("'" + id + ":");
if (index > -1)
IdWithNote = listNote[index];
return IdWithNote;
旧fashoned &明确(无正则表达式)此外,假设您只需要笔记的文本,而不是整个笔记结构。
string key = "'" + id + ":";
int noteStart = noteList.IndexOf(key);
if (noteStart >= 0)
{
int textStart = noteStart + key.Length;
int textEnd = noteList.IndexOf("'", textStart);
return noteList.Substring(textStart, textEnd - textStart);
}
return "";
var myId=2;
var t="'1:note1','2:note2'";
var query = t.Split(',').Select(c => c.Replace("'", "").Split(':')).
Where(c => c[0] == myId.ToString()).
Select(p=>p[1]).First();
下面是一些代码 - 你真的想要的路线是:retVal的= noteList.Substring(的startIndex,endIndex的 - 的startIndex);
int id = 8;
string noteList = "'8:the note i want','81:the note i do not want'";
string toFind = "'" + id.ToString() + ":";
int startIndex = noteList.IndexOf(toFind) + toFind.Length;
int endIndex = noteList.IndexOf("'", startIndex);
if (noteList.Contains(toFind))
{
retVal = noteList.Substring(startIndex, endIndex - startIndex);
}
else
{
retVal = "nothing found";
}
仅供参考:'string toFind =“'”+ id.ToString()+“:”;'你不需要ToString,'string toFind =“'”+ id +“:”;'会编译成相同事情。 – 2012-03-08 09:57:14
如果不是过分昂贵,我总是喜欢更具表现力和明确性。但是,谢谢你,我不知道它会编译成同样的东西(我知道它会编译只是不知道它会是完全一样的东西)。 – 2012-03-08 10:21:19
您可以按','拆分,然后按':'拆分,然后您就可以得到笔记。或者你可以使用正则表达式。 – arunes 2012-03-08 09:16:40
你想要整个笔记结构(id = text)还是仅仅是笔记的文本部分? – 2012-03-08 09:29:47