2

Is there a clever way to do the following string manipulation in C#?

I have any kind of string and im looking for a specified delimiter. The code should divide the string in words before and after delimiter and also include the delimiter. The delimiter could be several times in a row and also could be in the start or the end.

// PSEUDO CODE
string = "Hello****wide****world";
delimiter = "****";

// result should be a list<string> like
{"Hello","****","wide","****","world"}
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
Daarwin
  • 2,896
  • 7
  • 39
  • 69
  • How about using `Split` and then `Join` methods? – Sham Sep 24 '18 at 07:50
  • How would you split them? Doesnt split only take chars? – Daarwin Sep 24 '18 at 07:51
  • @Sham Join will result back to a string, not a collection... – Zohar Peled Sep 24 '18 at 07:51
  • no it doesnt! Its possible to split on a string. Still problem is that i need to keep the delimiter! – Daarwin Sep 24 '18 at 07:51
  • try this.. https://stackoverflow.com/questions/521146/c-sharp-split-string-but-keep-split-chars-separators – sicKo Sep 24 '18 at 07:56
  • The question identified as a duplicate is not a duplicate. There is a different output required. –  Sep 24 '18 at 08:28
  • Since I already made the code, there are two other parsing methods: https://pastebin.com/WKmjPNdz - you can configure the output; since you did not specify the behaviour for some cases... – Julo Sep 24 '18 at 08:36

6 Answers6

1

You can try to use Regex and the pattern is (\*{4}).

string data = "Hello****wide****world";

string[] words = Regex.Split(data, @"(\*{4})");
List<string> result = words.ToList();

NOTE

  1. * is a keyword in regex string, so you need to use \ to escape it.

c# online

D-Shih
  • 44,943
  • 6
  • 31
  • 51
0

i'll say split the string with the delimiter, then when creating the result add the delimiter after each item in the array.

string fullWord = "Hello****wide****world";
string delimiter = "****";

var listOfWords = fullWord.Split(delimiter);
List<string> result = new List<string>();

foreach(var item in listOfWords){
  result.Add(item);
  result.Add(delimiter);
}

return result;
wale A
  • 81
  • 1
  • 7
0

You can do it like this. In the end, you can iterate over the result.

string input = "Hello****wide****world";
string delimiter = "****";

var listOfWords = input.Split(new string[] { delimiter }, StringSplitOptions.None);
List<string> result = new List<string>();

foreach (var item in listOfWords)
{
    if (!item.Equals(listOfWords.Last()))
    {
        result.Add(item);
        result.Add(delimiter);
    }
    else
    {
        result.Add(item);
    }
}
Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
0

untested

string[] words = Regex.Split(originalString, @"(?=****)");
List<string> wordsLst = words.ToList();
sicKo
  • 1,241
  • 1
  • 12
  • 35
0

User Regex to do it :

string input = "Hello****wide****world";
string pattern = "(****)";

string[] substrings = Regex.Split(input, pattern);
Cornelis
  • 1,065
  • 8
  • 23
0
string fullWord = "Hello****wide****world";
string delimiter = "****";

var listOfWords = fullWord.Split(delimiter);
StringBuilder result = new StringBuilder("");
result.Append("{");
foreach(var item in listOfWords){
  if (!item.Equals(listOfWords.Last()))
  {
       result.Append($"\"{item}\",\"{delimiter}\",");
  }
  else
  {
       result.Append($"\"{item}\"");
  }
}
result.Append("}");
var stringResult=result.ToString();
habib
  • 2,366
  • 5
  • 25
  • 41