When I run this method I get the expected results, array of strings:
[0] L
[1] J
[2] E
[3] R
[4] K
[5] A
But I want to achieve this:
[0] LJ
[1] E
[2] R
[3] K
[4] A
I need to concatenate some characters into one string, for example:
LJ, NJ and DŽ are all needed to be as one string and not separate item in the array.
You can treat it a bit like a finite state machine and keep track of your current state for as far as needed. For example, the last character was L or something else.
enum State {L, Other}
State state = State.Other;
List<string> result = new List<string>();
for (int i = 0;i < mainWord.Length();i++)
{
char ch = mainWord[i];
if (state == State.L)
{
if (ch == 'J')
{
result.Add("LJ");
}
else
{
result.Add("L");
}
}
state = State.Other;
if (ch == 'L')
{
state = State.L;
}
else
{
result.Add("" + ch);
}
}
if (state == State.L)
{
result.Add("L");
}
Pretty long code, I must admit, but it does the job in a single pass over the input string.
not sure if it catches all the needed letters in all cases, but seems to work on this one,
using UnityEngine;
using System.Text.RegularExpressions;
public class FindLetters : MonoBehaviour
{
void Start()
{
// add here your special 2 letter words, and . matches for rest of the letters
string pattern = @"(LJ|NJ|DŽ|.)";
string input = @"LJERKALJNJDŽALVJGNYJUDRŽ";
MatchCollection results = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);
for (int i = 0, len = results.Count; i < len; i++)
{
Debug.LogFormat("index:{0} letter:{1}", i, results[i].ToString());
}
}
}