C# Stripping / converting one or more characters

南笙酒味 提交于 2019-12-01 04:16:54

问题


Is there a fast way (without having to explicitly looping through each character in a string) and either stripping or keeping it. In Visual FoxPro, there is a function CHRTRAN() that does it great. Its on a 1:1 character replacement, but if no character in the alternate position, its stripped from the final string. Ex

CHRTRAN( "This will be a test", "it", "X" )

will return

"ThXs wXll be a es"

Notice the original "i" is converted to "X", and lower case "t" is stripped out.

I looked at the replace for similar intent, but did not see an option to replace with nothing.

I'm looking to make some generic routines to validate multiple origins of data that have different types of input restrictions. Some of the data may be coming from external sources, so its not just textbox entry validation I need to test for.

Thanks


回答1:


All you need is a couple of calls to String.Replace().

string s = "This will be a test";
s = s.Replace("i", "X");
s = s.Replace("t", "");

Note that Replace() returns a new string. It does not alter the string itself.




回答2:


Does this what you want?

"This will be a test".Replace("i", "X").Replace("t", String.Empty)

Here is a simple implementation of the CHRTRAN function - it does not work if the string contains \0 and is quite messy. You could write a nicer one using loops, but I just wanted to try it using LINQ.

public static String ChrTran(String input, String source, String destination)
{
    return source.Aggregate(
        input,
        (current, symbol) => current.Replace(
            symbol,
            destination.ElementAtOrDefault(source.IndexOf(symbol))),
        preResult => preResult.Replace("\0", String.Empty));
}

And the you can use it.

// Returns "ThXs wXll be a es"
String output = ChrTran("This will be a test", "it", "X");

Just to have a clean solution - the same without LINQ and working for the \0 cases, too, and it is almost in place because of using a StringBuilder but won't modify the input, of course.

public static String ChrTran(String input, String source, String destination)
{
    StringBuilder result = new StringBuilder(input);

    Int32 minLength = Math.Min(source.Length, destination.Length);

    for (Int32 i = 0; i < minLength; i++)
    {
        result.Replace(source[i], destination[i]);
    }

    for (Int32 i = minLength; i < searchPattern.Length; i++)
    {
        result.Replace(source[i].ToString(), String.Empty);
    }

    return result.ToString();
}

Null reference handling is missing.

Inspired by tvanfosson's solution, I gave LINQ a second shot.

public static String ChrTran(String input, String source, String destination)
{
    return new String(input.
        Where(symbol =>
            !source.Contains(symbol) ||
            source.IndexOf(symbol) < destination.Length).
        Select(symbol =>
            source.Contains(symbol)
                ? destination[source.IndexOf(symbol)]
                : symbol).
        ToArray());
}



回答3:


Here was my final function and works perfectly as expected.

public static String ChrTran(String ToBeCleaned, 
                             String ChangeThese, 
                             String IntoThese)
{
   String CurRepl = String.Empty;
   for (int lnI = 0; lnI < ChangeThese.Length; lnI++)
   {
      if (lnI < IntoThese.Length)
         CurRepl = IntoThese.Substring(lnI, 1);
      else
         CurRepl = String.Empty;

      ToBeCleaned = ToBeCleaned.Replace(ChangeThese.Substring(lnI, 1), CurRepl);
   }
   return ToBeCleaned;
}



回答4:


To "replace with nothing", just replace with an empty string. This will give you:

String str = "This will be a test";
str = str.Replace("i", "X");
str = str.Replace("t","");



回答5:


A more general version as a string extension. Like the others this does not do a translation in place since strings are immutable in C#, but instead returns a new string with the replacements as specified.

public static class StringExtensions
{
    public static string Translate( this string source, string from, string to )
    {
        if (string.IsNullOrEmpty( source ) || string.IsNullOrEmpty( from ))
        {
            return source;
        }

        return string.Join( "", source.ToCharArray()
                                   .Select( c => Translate( c, from, to ) )
                                   .Where( c => c != null )
                                   .ToArray() );
    }

    private static string Translate( char c, string from, string to )
    {
        int i = from != null ? from.IndexOf( c ) : -1;
        if (i >= 0)
        {
            return (to != null && to.Length > i)
                      ? to[i].ToString()
                      : null;
        }
        else
        {
            return c.ToString();
        }
    }
}



回答6:


This is a case where I think using LINQ overcomplicates the matter. This is simple and to the point:

private static string Translate(string input, string from, string to)
{
    StringBuilder sb = new StringBuilder();
    foreach (char ch in input)
    {
        int i = from.IndexOf(ch);
        if (from.IndexOf(ch) < 0)
        {
            sb.Append(ch);
        }
        else
        {
            if (i >= 0 && i < to.Length)
            {
                sb.Append(to[i]);
            }
        }
    }
    return sb.ToString();
}



回答7:


  public static string ChrTran(string cSearchIn, string cSearchFor, string cReplaceWith)
        {
            string result = "";
            dynamic inArray = cSearchIn.ToCharArray();
            foreach (var caracter in inArray)
            {
                int position = cSearchFor.IndexOf(caracter);
                result = result + cReplaceWith.Substring(position, 1);

        }

        return result;
    }</pre>


来源:https://stackoverflow.com/questions/849245/c-sharp-stripping-converting-one-or-more-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!