Okay so my code looks kinda like this
// 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;
}
}
Now it's basically a system where for each id the user can set a note, and it will be saved in this format: 'id:note','id:note'
,
Now what I want to do is select that note somehow and return it, so I'd have to like select from "'" + id + ":"
until the '
If anyone knows how to do this, please help me out. Thanks
Using a Regex seems like the cleanest approach to me:
string regexFormat = "'{0}:(.*?)'";
Match match = Regex.Match(notelist, string.Format(regexFormat, id));
return match.Success ? match.Groups[1].Value : string.Empty;
Alternatively however, you could use string splitting:
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));
try
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;
As far I understood your code, following code will kinda give you a solution
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;
Old fashoned & clear (no regex) Also assumes you only want the text of the note, not the entire note structure.
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();
Here is a bit of code - the line you really wanted is: 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";
}
notelist.Substring(notelist.IndexOf("'" + id + ":"), (notelist.IndexOf("'") - notelist.IndexOf("'" + id + ":")));
this should do the trick, you can select the text by a substring into a new string. substring(startindex, lenght);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.