locked
how to find and delete a specific long substring from a string in c# RRS feed

  • Question

  • User-1257308419 posted

    i have a requirement in which i have a column and i am storing sorting sequence in it.

    1 single column stores sorting sequence(space is separating each sequence) in below manner

    ../assets/img/duas/nc.jpg1 ../assets/img/duas/wt.jpg2 ../assets/img/duas/sq.jpg3

    now i want some safe way through which i can safely and with confirmation delete a sorting sequence in case user deletes on of this 

    for eg i delete a picture for e.g(../assets/img/duas/wt.jpg2) i can handle that part where i need to perform join and delete this user saved pics but issue is that i can't think of any way through which i can safely first find and then  delete (../assets/img/duas/wt.jpg2) from above complete string.

    one thing more after deleting (../assets/img/duas/wt.jpg2) sequence will become 

    ../assets/img/duas/nc.jpg1 ../assets/img/duas/sq.jpg3 

    but i need the sequence to be rearranged like this

    ../assets/img/duas/nc.jpg1  ../assets/img/duas/sq.jpg2 

    is it possible?

    Thursday, August 13, 2020 1:58 PM

Answers

  • User1535942433 posted

    Hi Learner94,

    Accroding to your description,as far as I think,you could substring the number of jpg.And you could loop the list to replace every item.Just like this:

    String value = "../assets/img/duas/nc.jpg1 ../assets/img/duas/wt.jpg2 ../assets/img/duas/sq.jpg3";
                var sentences = new List<String>();
                int position = 0;
                int start = 0;
                // Extract sentences from the string.
                while (position >= 0 && start < value.Length)
                {
                    position = value.IndexOf("jpg", start);
                    if (position >= 0)
                    {
                        sentences.Add(value.Substring(start, position - start + 4).Trim());
                        start = position + 5;
    
                    }
                }
    
    
    //Remove;
                sentences.RemoveAt(1);
    
    //Renumber;
                var newsentences = new List<String>();
                for (int i = 0; i < sentences.Count; i++)
                {
                   
                    int jpgposition = 0;
                    int s = 0;
                    jpgposition = sentences[i].IndexOf("jpg", s);
                    if (jpgposition >= 0)
                    {
                      newsentences.Add(sentences[i].Replace(sentences[i].Substring(jpgposition - s + 3, 1).Trim().ToString(), (i+1).ToString()));
                    }
                }
                newsentences.ForEach(i => Console.Write("{0}\t", i));

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2020 9:16 AM

All replies

  • Thursday, August 13, 2020 5:58 PM
  • User1535942433 posted

    Hi Learner94,

    Accroding to your description,as far as I think,you could use IndexOf to get the 'jpg' position.And then you could use substring to get the complete string.Pushing them into  a list.Finally,you could use RemoveAt to delete items and join to add items.

    More details,you could refer to below codes:

     String value = "../assets/img/duas/nc.jpg1 ../assets/img/duas/wt.jpg2 ../assets/img/duas/sq.jpg3";
                var sentences = new List<String>();
                int position = 0;
                int start = 0;
                // Extract sentences from the string.
                while (position >= 0 && start < value.Length)
                {
                    position = value.IndexOf("jpg", start);
                    if (position >= 0)
                    {
                        sentences.Add(value.Substring(start, position - start + 4).Trim());
                        start = position + 5;
                    }
                }
                  
    
                // Display the sentences.
                foreach (var sentence in sentences)
                    Console.WriteLine(sentence);
    
                sentences.RemoveAt(1);
    
                string s = string.Join("../assets/img/duas/wt.jpg2",sentences,0,2);
                Console.WriteLine(s);

    Best regards,

    Yijing Sun

    Friday, August 14, 2020 6:06 AM
  • User-1257308419 posted

     string s = string.Join("../assets/img/duas/wt.jpg2",sentences,0,2);
                Console.WriteLine(s);

    thanks i just read some documentations regarding string searching in c# and its very easy as compared to c++.

    but issue is that i need to renumber the arrangement after deleting this particular string when found for eg

    ../assets/img/duas/nc.jpg1 ../assets/img/duas/wt.jpg2 ../assets/img/duas/sq.jpg3

    after deleting 

    ../assets/img/duas/wt.jpg2

    i need to rearrange the first one like below

    ../assets/img/duas/nc.jpg1  ../assets/img/duas/sq.jpg2 
    

    you se numbering needs to be done at the end of "jpg" and after deleting any pic i need to do the numbering again

    Friday, August 14, 2020 10:17 AM
  • User-1257308419 posted

      string s = string.Join("../assets/img/duas/wt.jpg2",sentences,0,2);
                Console.WriteLine(s);

    i just replaced above line with this line 

      string s = string.Join(" ", sentences);
    Console.WriteLine(s);

    this is what i get now

    ../assets/img/duas/nc.jpg1 ../assets/img/duas/sq.jpg3

    but i want this

    ../assets/img/duas/nc.jpg1 ../assets/img/duas/sq.jpg2

    as you can see i will have a very long string like this and every time a substring is deleted i need to renumber all the remaining strings

    i hope you get what i am trying to say

    Friday, August 14, 2020 1:02 PM
  • User1535942433 posted

    Hi Learner94,

    Accroding to your description,as far as I think,you could substring the number of jpg.And you could loop the list to replace every item.Just like this:

    String value = "../assets/img/duas/nc.jpg1 ../assets/img/duas/wt.jpg2 ../assets/img/duas/sq.jpg3";
                var sentences = new List<String>();
                int position = 0;
                int start = 0;
                // Extract sentences from the string.
                while (position >= 0 && start < value.Length)
                {
                    position = value.IndexOf("jpg", start);
                    if (position >= 0)
                    {
                        sentences.Add(value.Substring(start, position - start + 4).Trim());
                        start = position + 5;
    
                    }
                }
    
    
    //Remove;
                sentences.RemoveAt(1);
    
    //Renumber;
                var newsentences = new List<String>();
                for (int i = 0; i < sentences.Count; i++)
                {
                   
                    int jpgposition = 0;
                    int s = 0;
                    jpgposition = sentences[i].IndexOf("jpg", s);
                    if (jpgposition >= 0)
                    {
                      newsentences.Add(sentences[i].Replace(sentences[i].Substring(jpgposition - s + 3, 1).Trim().ToString(), (i+1).ToString()));
                    }
                }
                newsentences.ForEach(i => Console.Write("{0}\t", i));

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2020 9:16 AM