如何替换字符串中的字符串 [英] How do I replace string in string

查看:104
本文介绍了如何替换字符串中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天。所以我想问你如何改变我从JEditorPane获得的字符串中的子字符串。



我根据这个来制作和弦变换程序:C to C#, C#到D,D#到E,E到F,F到F#,F#到G,G到G#,...



我写的代码,但它总是从乞讨到结束。例如,如果我写在JEditorPane:



CD#E这一切都归于F#。如何才能进行1步更改(只需C到C#,D#到E,E到F)?



Good day. So I want to ask you how to change substring in a string that I got from JEditorPane.

Im making a program for chords changin according to this: C to C# , C# to D, D# to E , E to F, F to F# , F# to G , G to G#,...

I wrote code,but it allways goes from begginging till the end.For example, if I write in JEditorPane:

" C D# E " it all goes to F#. How can I just make changes for 1 step( just C to C# , D# to E , E to F)?

if (event.getSource()== ok)
			{
				Object contents = akordib.getSelectedItem();
				
				
				
				
				if (contents == "+1 step")
				{
					final = textPanel.getText();
					
					if (final.contains(" C ") == true)
					{
						final = final.replaceAll(" C" , " C#");
						textPanel.setText(final);
					}
					
					if (final.contains(" C# ") == true)
					{
						final = final.replaceAll(" C# ", " D ");
						textPanel.setText(final);
					}
				}	
    				}

推荐答案

以相反的顺序执行替换。你可能想要使用中间值。



你真的需要先查看字符串是否包含值吗?



我建议只在最后执行setText。





如你所知,你我会多次扫描弦乐;我会编写一个只扫描一次的自定义方法。
Perform the replacements in reverse order. And you may want to use intermediate values.

And do you really need to check to see whether or not the string contains the value first?

I recommend performing the setText only at the end.


As you have it, you'll be scanning the string a great many times; I would write a custom method that scans it only once.


这是一个用C#实现的快速粗略一次通过算法(我不懂Java)。

要改变/改进的是字典(查找表),并且如所写的,它要求字符串以空格结尾(作为练习留下)。

你需要一个不同的字典步骤。



Here's a quick rough one-pass algorithm implemented in C# (I don't know Java).
Things to be altered/improved are the Dictionary (lookup table) and, as written, it requires that the string end with whitespace (left as an exercise).
You would need a different Dictionary for each step.

System.Collections.Generic.Dictionary<string,string> rep =
  new System.Collections.Generic.Dictionary<string,string>() ;
rep [ "C"  ] = "C#" ;
rep [ "C#" ] = "D"  ;
rep [ "D#" ] = "E"  ;
rep [ "E"  ] = "F"  ;
rep [ "F"  ] = "F#" ;
rep [ "F#" ] = "G"  ;
rep [ "G"  ] = "G#" ;

string muse = " C D# E " ;

System.Text.StringBuilder token  = new System.Text.StringBuilder ( muse.Length ) ;
System.Text.StringBuilder result = new System.Text.StringBuilder ( muse.Length ) ;

for ( int i = 0 ; i < muse.Length ; i++ )
{
  char ch = muse [ i ] ;

  if ( !System.Char.IsWhiteSpace ( ch ) )
  {
    token.Append ( ch ) ;
  }
  else
  {
    if ( token.Length > 0 )
    {
      string tok = token.ToString() ;

      if ( rep.ContainsKey ( tok ) )
      {
        result.Append ( rep [ tok ] ) ;
      }
      else
      {
        result.Append ( tok ) ;
      }

      token.Length = 0 ;
    }

    result.Append ( ch ) ;
  }
}


这篇关于如何替换字符串中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆