String[] values = line.split(",");
Long locId = Long.parseLong(replaceQuotes(values[0]));
String country = replaceQuotes(values[1]);
String region = replaceQuotes(values[2]);
String city = replaceQuotes(values[3]);
String postalCode = replaceQuotes(values[4]);
String latitude = replaceQuotes(values[5]);
String longitude = replaceQuotes(values[6]);
String metroCode = replaceQuotes(values[7]);
String areaCode = replaceQuotes(values[8]);
//...
public String replaceQuotes(String txt){
txt = txt.replaceAll("\"", "");
return txt;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用上面的代码用这种格式的数据解析CSV:
828,"US","IL","Melrose Park","60160",41.9050,-87.8641,602,708
Run Code Online (Sandbox Code Playgroud)
但是,当我遇到一系列数据时,如下所示我得到了 java.lang.ArrayIndexOutOfBoundsException: 7
1,"O1","","","",0.0000,0.0000,,
Run Code Online (Sandbox Code Playgroud)
这是否意味着我甚至试图访问该值时values[7],会抛出异常?
如果是这样,我如何解析文本行的那个位置不包含数据的行?
java csv exception-handling string-parsing indexoutofboundsexception
可以说我有一堆坏话:
$badwords = array("one", "two", "three");
Run Code Online (Sandbox Code Playgroud)
随机字符串:
$string = "some variable text";
Run Code Online (Sandbox Code Playgroud)
如何创建此循环:
if (one or more items from the $badwords array is found in $string)
echo "sorry bad word found";
else
echo "string contains no bad words";
Run Code Online (Sandbox Code Playgroud)
示例:
如果$string = "one fine day" or "one fine day two of us did something",用户应该看到抱歉坏词找到的消息.
如果$string = "fine day",用户应该看到字符串包含没有坏词的消息.
据我所知,你不能preg_match从数组.有什么建议吗?
我想使用Python从电子邮件地址中提取用户名.我想到的解决方案是将电子邮件地址附加到列表中,找到@符号的索引,然后对列表进行切片,直到找到索引.
我的代码是:
#!/usr/bin/env python<br/>
email = raw_input("Please enter your e-mail address: ")
email_list = []
email_list.append(email)
at_symbol_index = email_list.index("@")
email_username = email_list[0:at_symbol_index]
print email_username
Run Code Online (Sandbox Code Playgroud)
但是,每次我运行脚本时,它都会返回错误:
ValueError: list.index(x): x not in list
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?
我想验证用户输入字符串的数字范围是1300到1500.
我试过了 Regex regxUsrInput = new Regex(@"^(?:[1-9]|1|3[0-9]|0[0-9]|0[0-9]|1500)$");
..但这并没有验证我的预期方式.
我愿意接受任何建议,包括替代方案.谢谢
如果重要:我的应用程序是用C#编写的Windows窗体应用程序.用户输入来自标准TextBox控件.
我正试图从字符串中获取浮点数或整数.为此,这是我的测试用例..
1) str="IN100RINR" Output = 100;
2) str="IN100RINR" Output = 100;
3) str="100INR" Output = 100;
4) str="100.50INR" Output = 100.50;
5) str="IN100.50R" Output = 100.50;
6) str="INR100.50" Output = 100.50;
7) str="INR100INRINR20.500INR" Output = 100
Run Code Online (Sandbox Code Playgroud)
这一切都在我的程序中工作,但案例7不起作用..返回100.500
这是我的代码......
Pattern pattern = Pattern.compile("(\\d+)");
String str="INR100INRINR20.500INR", amount="", decimal="";
if(str.contains(".")){
String temp= str.substring(str.indexOf(".")+1);
Matcher matcher = pattern.matcher(temp);
if(matcher.find()){
decimal = matcher.group();
}
}
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
if(decimal != ""){
amount=matcher.group()+"."+decimal;
}else{
amount = matcher.group();
}
System.out.println(Float.valueOf(amount));
}
Run Code Online (Sandbox Code Playgroud) 我的印象是java支持unicode字符.我做了这个测试,遗憾地发现它失败了.问题是为什么?这是一个错误还是有记录的地方?
// MATHEMATICAL SANS-SERIF ""
String unicodeNum6 = "\uD835\uDFE8";
int codePoint6 = unicodeNum6.codePointAt(0);
int val6 = Character.getNumericValue(codePoint6);
System.out.println("unicodeNum6 = "+ unicodeNum6
+ ", codePoint6 = "+ codePoint6+ ", val6 = "+val6);
int unicodeNum6Int = Integer.parseInt(unicodeNum6);
Run Code Online (Sandbox Code Playgroud)
这失败了 Exception in thread "main" java.lang.NumberFormatException: For input string: ""
意思是我认为,因为println工作并打印预期的行:
unicodeNum6 = , codePoint6 = 120808, val6 = 6
Run Code Online (Sandbox Code Playgroud)
所以Java完全知道unicode字符的数值但不在parseInt中使用它.
有人能说明为什么会失败吗?
救命!我在Excel中有一个记录列表,我正在复制/粘贴到ASP.NET网页中.从那里,C#代码解析记录.
下面的代码适用于其中一个名称,但不适用于另一个名称.但是,如果我使用键入的空格复制/替换Excel中的空白区域,或者如果我实际退格并使用键盘将名称键入网页,则它确实有效.
这就好像Excel在我为此记录的空间给出的文件中有一些奇怪的鬼字符.我已经粘贴在Notepad ++中并显示了所有字符,我在这里看不到任何特殊的记录.
这个工作并检测空间:Carolyn Bentivegna
这个不是:Allan D. Blake
if (fullName.IndexOf(" ") > -1)
Run Code Online (Sandbox Code Playgroud) 我需要解析来自 Android 设备内置 GPS 接收器的 NMEA 数据。我每秒以字符串形式接收此数据几次。我很好奇是否可以在没有垃圾收集分配或解析字符串的情况下做到这一点,这是我可以GC.Collect()问心无愧的时刻之一?
正是我需要调用string.split()和其他一些方法,如Substring()和结果转换为 with double.Parse()。
我试图通过转换为来做到这一点,char[]但这样一来 GC 分配就更大了。
GPS NMEA 数据有很多句子,我需要每秒解析 2-3 个句子。下面是解析这句话之一的示例代码 - $GPRMC
例句:
$ GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62 $GPGGA,123519,4807.038,N,01131.000,4M.5,4M ,M,,*47 $GPGSA,A,3,32,27,03,193,29,23,19,16,21,31,14,,1.18,0.51,1.07*35
// Divide the sentence into words
string[] Words = sSentence.split(',');
// Do we have enough values to describe our location?
if (Words[3] != "" & Words[4] != "" &
Words[5] != "" & Words[6] != "")
{
// example 5230.5900,N
// 52°30.5900\N
// Yes. Extract …Run Code Online (Sandbox Code Playgroud) 我通过 Flask-SQLAlchemy 使用 SQLAlchemy 作为 Web 应用程序的 ORM。
我想自动前导和尾随带空格(例如 str.strip在分配给任何字符串字段时)。
执行此操作的一种方法如下,但需要为每个字符串字段指定它:
class User(db.Model):
_email = db.Column('email', db.String(100), primary_key=True)
@hybrid_property
def email(self): return self._email
@email.setter
def email(self, data): self._email = data.strip()
Run Code Online (Sandbox Code Playgroud)
我想对每个 String 字段更通用地执行此操作(无需为每个字段编写上述内容)。
我是编程新手,并且刚开始学习第一门课程,即编程基础知识,而在目前的家庭作业中,我遇到了一个我无法解决的问题。
问题是-“一袋饼干可容纳40个饼干。袋中的卡路里信息声称袋中有10份,每份等于300卡路里。创建一个应用程序,让用户输入自己或他或她的Cookie数量她实际上吃了,然后报告了所消耗的卡路里数量。”
我的表格:

我在未调试的情况下运行时遇到的错误:

//下面是我的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calorie_Counter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int cookies = int.Parse(textBox1.Text);
int calories = (cookies * 75);
textBox2.Text = calories.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud) string-parsing ×10
c# ×4
java ×3
python ×2
regex ×2
.net ×1
algorithm ×1
asp.net ×1
copy-paste ×1
csv ×1
excel ×1
exception ×1
integer ×1
orm ×1
php ×1
preg-match ×1
sqlalchemy ×1
string ×1
unicode ×1
winforms ×1