我有一个相同的字符串
a|b|c|d|e|....|n
Run Code Online (Sandbox Code Playgroud)
我需要在javascript中获取此字符串中的最后一项.此字符串中可能有n个项目.提前致谢.
我被要求解决 C# 中的一个问题,以在一行中获取来自控制台的“n”个用户输入的总和,这些输入以空格分隔。
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[n];
int sum = 0;
for(int i = 0; i < n; i++) {
arr[i] = Convert.ToInt32(Console.ReadLine());
sum += arr[i];
}
Console.WriteLine("{0}",sum);
Run Code Online (Sandbox Code Playgroud)
如何修改此代码以从空格分隔的输入中获取预期的输出?此外,这些值需要存储在数组中。
输入:
5
1 2 3 4 5
输出:
15
我有一个类从文件中读取特定列并将其插入到数组中。我想将该数组用逗号分隔符打印在同一行上。
下面是我的代码:
public static void getArray(int Column, File path, String Splitter) throws IOException
{
List<String> lines = Files.readAllLines(path.toPath(), StandardCharsets.US_ASCII);
for (String line : lines)
{
String[] array = line.split(Splitter);
//Will return all elemetns on the same line but without any separation, i need some kind of separation
// if i use System.out.print(array[Column]+" ,");
// i will always get a , at the end of the line
System.out.print(array[Column]);
}
}
getArray(3, file, "|");
Run Code Online (Sandbox Code Playgroud)
当前输出为:
abcdefg
期望的输出是:
a,b,c,d,e,g
我想删除某些 UITableViewCell separators(不是全部)。
我在 UI 中看到它们,但不明白为什么当我打印出子视图层次结构时它们没有被列出?
for view in cell.subviews {
DDLogDebug(String(describing: type(of: view)))
}
Run Code Online (Sandbox Code Playgroud)
这似乎发生在初始视图中。如果我将 UITableView 单元格滚动到远处然后再返回,UITableViewCell 的分隔符会神奇地出现在子视图日志打印输出中。这里发生了什么?
注意: 我只在模拟器上测试过,因为我目前没有 iPhone X。
我有一个包含这些数据的log.txt文件:
2022-08-25 13:16----------817650|xx:xx:xx:xx:xx:xx|user1|10
2022-08-25 13:16----------817856|xx:xx:xx:xx:xx:xx|user1|5
Run Code Online (Sandbox Code Playgroud)
我想比较最后两行的最后一个值(10 和 5),但我在识别这些行时遇到了困难。
<?php
$textCnt = "./log.txt";
$contents = file_get_contents($textCnt);
$arrfields = explode( "|" , $contents);
echo $arrfields[3];
?>
Run Code Online (Sandbox Code Playgroud)
我懂了:
10 2022-08-25 13:16----------817856
Run Code Online (Sandbox Code Playgroud)
代替
10
Run Code Online (Sandbox Code Playgroud) 我应用此解决方案来解决我将文件导出到.CSV和Excel的编码问题并且它成功运行,但现在又出现了另一个问题:
我在PHP中使用的标签("\ t")作为Excel文件的分隔符停止工作.在我解决上一个问题之前它正在工作.当我打开excel文件时,显示如下:
"ColumnAColumbBColumnC"(全部在一起).
如果导出它像csv ('Content-type: text/csv; charset=UTF-8')我有成功,但不是Excel(Content-type: application/vnd.ms-excel; charset=UTF-8).
对此有何解决方案?
注意:使用","作为分隔符对我来说不是一个好的解决方案,因为在某些字段中我的值为",".
给定一个包含字符串"Hello World"的文件(注意'Hello'和'World'之间有一个空格).
int main()
{
ofstream fout("test.txt");
fout.write("Hello World", 12);
fout.close();
ifstream fin("test.txt");
vector<string> coll((istream_iterator<string>(fin)),
(istream_iterator<string>()));
// coll contains two strings 'Hello' and 'World' rather than
// one string "Hello World" that is just I want.
}
Run Code Online (Sandbox Code Playgroud)
换句话说,我希望istream中的字符串只能用'\n'而不是'','\n'等分隔.
我应该怎么做?