#string replace

1 messages · Page 1 of 1 (latest)

noble cobalt
#

how whould i remove the underscore in search when there is no underscore in command. i came this far but cant seem to figure out how to remove the underscore without removing at a specific index or removing all. The search is dynamic normally but for demonstration its static here.


            var updatedSearch = "Debug_Log_Test"
            var updatedCommand = "DebugLog_Test"
            if (updatedSearch.Any(ch => ch != '_'))
            {
                if(updatedCommand.Any(ch => ch == '_'))
                {
                     remove the underscore from the search but only at the index where at
                }
            }
toxic summit
#

Oh god please

#

Do not use Any here

#

LINQ is completely unnecessary

#

Tbf, it's always unnecessary

#

And if you want to remove the underscore.. just replace it with empty string

noble cobalt
#

but i only want to remove the underscore where there shouldnt be one

#

Like

#

Debug*_*Log_Test

#

then i would only want to remove the first underscore

toxic summit
#
  • loop through the string (for loop, not foreach, because you'll want to know the index)
  • compare with char at the same index in the desired string
  • if source string has an underscore, but desired string doesn't, strip it
  • continue stripping until chars match at the same index
  • move on to next char
#

NB: this will only work for strings which are similar. if you have one string "hello" and another "fubar", since no characters match it'll strip every char

#

to make it easier, I recommend using a StringBuilder

#

since that represents a mutable string. you can insert/remove chars at any point, and only get the final result when you call .ToString()

#

Psuedocode of what you need to do:

for i = 0 to source.Length
    while source[i] == '_' and desired[i] != '_'
        source.Remove(i, 1)

assuming source is a StringBuilder (which has a Remove method)

#

obviously do bound check to make sure you don't access beyond the length of desired, and such

#

I mean it's pseudocode so y'know. take with a grain of salt

ripe merlin
#

Could also use something like cs int index = input.IndexOf('_'); return input[0..index] + input[(index + 1)..^0];if only the first _ is supposed to be removed.

toxic summit
#

Oh you could. Wouldn't work for a general solution though

#

maybe source string is hello_world_string and reference/desired string is hello_worldstring
in that case it should strip the 2nd underscore, not the first

#

if I understand OP's question correctly

noble cobalt
#
            StringBuilder sb = new StringBuilder();

            for (int k = 0; k < updatedSearch.Length; k++)
            {
                if (updatedCommand[k].Equals('_'))
                {
                    if (!updatedSearch[k].Equals('_')) sb.Append("_");
                }

                sb.Append(updatedSearch[k]);
            }

            updatedSearch = sb.ToString();
#

came up with this

toxic summit
#

fair. does it work?

noble cobalt
#

let me test

toxic summit
#

looks like it should

#

a bit verbose though

noble cobalt
#

yeah

#

will try refactor

#

in a bit

toxic summit
#

oh right my example doesn't take into account adding underscores

#

it only removes them

noble cobalt
#

this doesnt work yet

toxic summit
#

perhaps something like

var builder = new StringBuilder(sourceString);

for (int index = 0; index < builder.Length && index < desiredString.Length; index++)
{
    if (builder[index] == desiredString[index]) continue;

    if (desiredString[index] == '_')
    {
        // reference string has _ but source does not. add it
        builder.Insert(index, '_');
    }
    else if (builder[index] == '_')
    {
        // source string has _ but reference does not. remove it
        builder.Remove(index--, 1); // decrement because we need to account for the fact we're backtracking
    }
}

string result = builder.ToString();

I wrote this code in discord so I have zero idea if it works or tbh if it even compiles var
but maaaaybe something like that

#

The reason for the decrement is because of the removal of a char.
Suppose you have _foobar and the reference string is foobar - in which case it should strip the underscore.
The call to builder.Remove(index--, 1) will first remove 1 char at index 0. Index is then decremented to -1
But the loop continuation clause has index++, which will bump it back to 0

Since you removed the underscore, there's a new "first char" to check. You have to decrement because of the backtracking

noble cobalt
#

your code works great

#

thanks

toxic summit
#

cool 👍

#

honestly I'm surprised kekw

#

I did not test it

noble cobalt
#

let me show you the result

#

you can see it updating fine in the recomendations list

toxic summit
#

nice

#

An alternative solution to this would be to strip any punctuation from both strings and just check that

#

you can use char.IsLetter to filter only letters, ignoring numbers and punctuation and whitespace