簡體   English   中英

從特定字符中選擇字符串直到c#中的某個字符

[英]Select string from certain character until certain character in 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));

嘗試

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&clear(沒有正則表達式)還假設您只需要注釋的文本,而不是整個注釋結構。

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";
        }
notelist.Substring(notelist.IndexOf("'" + id + ":"), (notelist.IndexOf("'") - notelist.IndexOf("'" + id + ":")));

這應該可以解決問題,您可以通過子字符串將文本選擇為新字符串。 substring(startindex,lenght);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM