我正在制作WinForm C#应用程序。我有一个按钮,它将搜索一个字符串并在其旁边给出值。这是我的代码:
WebRequest Request = WebRequest.Create(Settings.Default.FoodList);
WebResponse Response = Request.GetResponse();
StreamReader sr = new System.IO.StreamReader(Response.GetResponseStream());
string foods = sr.ReadToEnd();
string item = TXTSearchItem.Text;
try
{
var startIndex = foods.IndexOf(item + "-", StringComparison.CurrentCultureIgnoreCase);
var dashIndex = foods.IndexOf("-", startIndex);
var endIndex = foods.IndexOf("\r\n", startIndex);
var foodName = foods.Substring(startIndex, dashIndex - startIndex);
var footCount = foods.Substring(dashIndex + 1, endIndex - dashIndex - 1);
MessageBox.Show($"Item {foodName} is worth {footCount} SmartPoints.", "Item search", MessageBoxButtons.OK, info);
}
catch (Exception Ex)
{
MessageBox.Show("Item was not found.", "Item search", MessageBoxButtons.OK, warning);
}
由于某种原因,它不会获得最后一个字符串。这是字符串包含的内容:
goldfish-4
eggs-0
banana-0
chicken-0
apple-0
grapes-0
如果我输入grapes
,它将表示找不到该项目。但是,如果我有一个显示食物字符串的MessageBox,它将正确显示它。请帮忙。谢谢
答案 0 :(得分:0)
也许,在这里使用import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.*;
public class InternalFrameTest {
public static void main(String args[]) {
JFrame frame = new JFrame("Internal Frame Listener");
Container contentPane = frame.getContentPane();
JLayeredPane desktop = new JDesktopPane();
desktop.setOpaque(false);
desktop.add(createLayer("One"), JLayeredPane.POPUP_LAYER);
desktop.add(createLayer("Two"), JLayeredPane.DEFAULT_LAYER);
desktop.add(createLayer("Three"), JLayeredPane.PALETTE_LAYER);
contentPane.add(desktop, BorderLayout.CENTER);
frame.setSize(300, 300);
frame.show();
}
static JInternalFrame createLayer(String label)
{
return new SelfInternalFrame(label);
}
static class SelfInternalFrame extends JInternalFrame
{
SelfInternalFrame mySelf = this;
InternalFrameListener listener = new InternalFrameListener()
{
public void internalFrameActivated(InternalFrameEvent e) {
dumpInfo("Activated", e);
InternalFrameEvent e5 = new InternalFrameEvent(mySelf, InternalFrameEvent.INTERNAL_FRAME_DEICONIFIED);
mySelf.dispatchEvent(e5);
}
public void internalFrameClosed(InternalFrameEvent e) {
dumpInfo("Closed", e);
}
public void internalFrameClosing(InternalFrameEvent e) {
dumpInfo("Closing", e);
}
public void internalFrameDeactivated(InternalFrameEvent e) {
dumpInfo("Deactivated", e);
}
public void internalFrameDeiconified(InternalFrameEvent e) {
dumpInfo("Deiconified", e);
}
public void internalFrameIconified(InternalFrameEvent e) {
dumpInfo("Iconified", e);
}
public void internalFrameOpened(InternalFrameEvent e) {
dumpInfo("Opened", e);
}
private void dumpInfo(String s, InternalFrameEvent e) {
System.out.println("Source: " + e.getInternalFrame().getName()
+ " : " + s);
}
};
public SelfInternalFrame(String s) {
getContentPane().add(new JLabel(s, JLabel.CENTER),
BorderLayout.CENTER);
setName(s);
addInternalFrameListener(listener);
setBounds(50, 50, 100, 100);
setResizable(true);
setClosable(true);
setMaximizable(true);
setIconifiable(true);
setTitle(s);
setVisible(true);
}
}
}
更容易?
可以通过这种方式完成:
string.Split()
答案 1 :(得分:0)
字符串不是存储这些信息的好地方。如果这些商品的价格是唯一的(没有香蕉= 1,香蕉= 2)。您应该将其存储在反映这种唯一性的数据结构中,以简化搜索价格。
var input = @" goldfish-4
eggs-0
banana-0
chicken-0
apple-0
grapes-0";
string pattern = @"(?<key>\w+)-(?<value>\d+)";
RegexOptions options = RegexOptions.Multiline;
var r = Regex.Matches(input, pattern, options)
.Cast<Match>()
.ToDictionary(
m => m.Groups["key"].Value,
m => int.Parse(m.Groups["value"].Value)
);
:正则表达式说明:
然后轻松访问数据。甚至添加新产品或更改价格。而且,如果您需要再次将其存储在文件中,请使用一些序列化。