首页 \ 问答 \ java 判断字符串A包含多少个指定字符串

java 判断字符串A包含多少个指定字符串

String A="abababababab"; String B = "ab"; String C = "aba"; String D ="abab"; 判断字符串A中有多少个B,有多少个C,多少个D; 谢谢各位大佬帮忙解决。
更新时间:2023-02-01 13:02

最满意答案

我有一个我正在使用的程序,它使用String.format(...)和格式字符串:

// in the constants section
private static final String DISPLAY_FORMAT_STR = "%02d:%02d:%02d:%01d";   


private void showTimeLeft() {
  int oldMin = min;
  hours = (int) (deltaTime / (MS_PER_SEC * SEC_PER_MIN * MIN_PER_HR));
  min = (int) (deltaTime / (MS_PER_SEC * SEC_PER_MIN) % MIN_PER_HR);
  sec = (int) (deltaTime / (MS_PER_SEC) % SEC_PER_MIN);
  msec = (int) (deltaTime % MS_PER_SEC);

  String displayString = String.format(DISPLAY_FORMAT_STR, hours, min, sec,
        msec / 100);
  displayField.setText(displayString);

  // ... etc...

否则,如果您正在处理Date对象(我不是),您可以使用SimpleDateFormat对象来更好地格式化输出。


I've got a program that I'm working on now that uses String.format(...) and a format String for this:

// in the constants section
private static final String DISPLAY_FORMAT_STR = "%02d:%02d:%02d:%01d";   


private void showTimeLeft() {
  int oldMin = min;
  hours = (int) (deltaTime / (MS_PER_SEC * SEC_PER_MIN * MIN_PER_HR));
  min = (int) (deltaTime / (MS_PER_SEC * SEC_PER_MIN) % MIN_PER_HR);
  sec = (int) (deltaTime / (MS_PER_SEC) % SEC_PER_MIN);
  msec = (int) (deltaTime % MS_PER_SEC);

  String displayString = String.format(DISPLAY_FORMAT_STR, hours, min, sec,
        msec / 100);
  displayField.setText(displayString);

  // ... etc...

Otherwise if you're dealing with a Date object (I'm not), you could use a SimpleDateFormat object to better format your output.

相关问答

更多
  • 我有一个我正在使用的程序,它使用String.format(...)和格式字符串: // in the constants section private static final String DISPLAY_FORMAT_STR = "%02d:%02d:%02d:%01d"; private void showTimeLeft() { int oldMin = min; hours = (int) (deltaTime / (MS_PER_SEC * SEC_PER_MIN * MI ...
  • 使用SwingWorker可能更容易(从长远来看)。 它提供了许多有用的方法来更新UI(从Event Dispatching Thread的上下文),同时允许在后台继续执行... import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridBagLayout; import java.beans.PropertyChangeEvent; import java ...
  • 让我们退后一步,你似乎需要...... 一个“定时器”,它有一个固定的持续时间,需要产生某种“滴答”事件,让观察者知道它已更新 某种“定时器”“列表”可以在每个“定时器”完成时按顺序运行它们 你想要小心的是,不把对象的引用传递给其他真正没有责任改变它们或对它们感兴趣的对象。 相反,你应该考虑使用某种Observer模式 ,它允许你以分离的方式监视对象的变化。 这是“单一责任原则” 在这种情况下, Timer不关心你的用户界面,它关心在特定的时间段内以某个间隔运行,当它“滴答”并且完成时产生事件,就是这样。 ...
  • 将picTimer的声明移动到类级别,以便不需要在构造函数的本地范围内初始化该变量 Move the declaration for picTimer to class level so that the variable is not required to be initialized within the local scope of the constructor
  • 由于渲染是微不足道的,我发现你的例子的这种变化非常平滑。 渲染时间远低于半毫秒,因此12毫秒周期(〜83 Hz)有足够的时间来完成一帧,通常少于一个内核的10%。 随着渲染时间的增长,计时器线程变得饱和,并且事件被合并。 由于渲染与垃圾收集和外部处理需求竞争,效果在单个内核上得到放大。 Java不是一个实时系统,并不是所有的调度程序都是相同的。 如您在这里所建议的那样,您当然想要分析您的实际代码,以查看与波动性能之间的任何关联。 另一种方法是延长周期(降低频率)以满足渲染期限,并在moveImage()使用 ...
  • 首先看看如何 在Swing中 使用Swing Timers和Concurrency,了解如何使用Swing Timer以及为什么应该这样做。 接下来,在你的课堂上,创建一个timer属性... private Timer timer; 然后在你的构造函数中,创建一个Timer的实例... timer = new Timer(5, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ...
  • 通常,我们建议使用Swing定时器而不是通用定时器来执行与GUI相关的任务,因为Swing定时器都共享相同的预先存在的定时器线程,并且GUI相关任务将自动在事件分派线程上执行。 但是,如果您不打算从计时器触摸GUI ,或者需要执行冗长的处理,则可以使用通用计时器 。 文档: http : //docs.oracle.com/javase/tutorial/uiswing/misc/timer.html 我应该推荐使用util.Timer 。 In general, we recommend using Sw ...
  • 如果预定代码正在修改GUI,最好使用Swing Timer 。 否则,如果您使用非Swing Timer ,则需要使用cancel()将其关闭。 Better to use a Swing Timer if the scheduled code is modifying the GUI. Otherwise if you are using the non-Swing Timer than you need to shut it down with cancel().
  • 您的已发布代码中存在范围问题:您的Timer变量tm在开始按钮的actionPerformed方法中声明,因此仅在该方法中可见 。 因此,当在该方法之外时,您无法获得可行的参考。 解决方案是将类级别的变量声明为私有实例(非静态)变量,并且只在开始按钮的动作侦听器中对其调用start() 。 这将使变量在整个类中可见,并且停止按钮的侦听器应该能够调用其方法。 例如, package pkg3; import java.awt.event.ActionEvent; import java.awt.event. ...
  • javax.swing.Timer可能以守护程序线程开始:它不会保持jvm活动,主要结束,jvm退出。 它将计时器事件发布到GUI事件队列,该队列在第一个对话框或框架可见时开始。 如果根本不需要窗口系统,则必须创建JFrame并使其可见或使用java.util.Timer 。 以下代码显示了如何使用java.util.Timer : import java.util.Timer; import java.util.TimerTask; public class TimerDemo extends Time ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。