我有一个字节数组,每个字节都用 0xFF 初始化:
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = 0xFF;
}
Run Code Online (Sandbox Code Playgroud)
一旦这个字节数组填充了有效数据,我需要提取存储在偏移量 192 处的 ASCII 字符串,长度最多可达 32 个字符。我这样做是这样的:
ASCIIEncoding enc = new ASCIIEncoding();
stringToRead = enc.GetString(buffer, 192, 32);
Run Code Online (Sandbox Code Playgroud)
这是可行的,但我需要去掉包含 0xFF 的尾随字节,以避免字符串看起来像“John Smith?????????????????????”。.NET 中有提供这种功能的函数吗?也许是类似 String.TrimEnd() 函数的东西,或者我正在寻找正则表达式来执行此操作?
我需要从字符串中删除所有标签并使其不带空格。
我有字符串
"<span class="left_corner"> </span><span class="text">Adv</span><span class="right_corner"> </span>"
Run Code Online (Sandbox Code Playgroud)
使用 strip_tags 后,我得到字符串
" Adv "
Run Code Online (Sandbox Code Playgroud)
使用修剪功能我不能删除空格。
JSON 字符串看起来像“\u00a0...\u00a0”。
请帮我删除这个空格。
我有一个数据框,它由每个观察的变量列组成,这些列是行。我需要修剪这些数据以删除不需要的观察结果。
我通常如何做到这一点 -
trimmed_stats <- ddply(.data = data, .(pos), subset,
!AvgGFP > 100 &
!AvgRFP > 60 &
!Area < 220 &
!Area > 2000 &
!DeviationsRFP > 20 &
!DeviationGFP > 20)
Run Code Online (Sandbox Code Playgroud)
...基本上删除不符合特定标准的数据。
但是,当我查看多个数据集时,这些特定数字会因数据集而异。
我想要做的是使用与平均值的标准偏差执行“修剪”。
例如,让我们采用一个简单的数据框,每个观察值都有一个变量 -
p <- data.frame(obs = c(1:1000), var1 = rnorm(1000, 0 , 5))
sd(p[,2])
[1] 4.91213
Run Code Online (Sandbox Code Playgroud)
所以 1 个标准差是 4.91213,如何删除 var1 不在均值 1 个标准差范围内的所有行?
我从文本文件中读入并分配变量,就好像它们是带有列表的数组一样。我通过在换行符上爆炸来做到这一点。
但是,我还想修剪输入两侧的任何空白。根据我的理解和测试,这正是 trim() 所做的。
我想缩短它,以便它减少重复和更容易阅读。
$config = file_get_contents('scripts/secure/config.txt');
list($host, $dbname, $username, $password) = explode ("\n", $config);
$host = trim($host);
$dbname = trim($dbname);
$username = trim($username);
$password = trim($password);
Run Code Online (Sandbox Code Playgroud)
我尝试了几种不同的方法,但似乎都不起作用。我上面的方法确实有效,但我正在寻找一种单行方法。
Every time I try doing the trim method and the lowercase method it doesn't show the way I want it.
sentence.trim();
sentence.toLowerCase();
System.out.println("\"" + sentence + "\"" + " ---> ");
Run Code Online (Sandbox Code Playgroud)
For example, if I input " Hello World! ", it will print out " Hello World! " and not use any of the methods.
在用户控制台中我有bash:
$ echo $SHELL
/bin/bash
$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Run Code Online (Sandbox Code Playgroud)
我在文件 test.sh 中有代码:
$ cat test.sh
aaa='---aa-aa---'
echo "${aaa}"
echo 'does not work...'
# trim "-"
echo ${aaa/+(-)}
echo ${aaa%%+(-)}
echo 'works for one symbol...'
echo ${aaa%-}
echo ${aaa/-}
Run Code Online (Sandbox Code Playgroud)
最后两行工作正常,但前面的两行工作正常。
$ bash test.sh
---aa-aa---
does not work...
---aa-aa---
---aa-aa---
works for one symbol...
---aa-aa--
--aa-aa---
Run Code Online (Sandbox Code Playgroud)
同时,如果您尝试使该控制台正常工作:
$ aaa='---aa-aa---'
$ echo ${aaa/+(-)}
aa-aa---
$ echo ${aaa%%+(-)}
---aa-aa
Run Code Online (Sandbox Code Playgroud)
那么,为什么它在脚本中不起作用呢?
我想弄清楚如何\n用最多两个中断替换多个中断 ( )。我使用trimmingCharacters(in:)删除字符串周围的所有空格和新行,但我不知道如何删除字符串中的空格,或者说:我怎么能替换超过 2 个中断最多2次休息?
如果用户在 textView 中写入单词之间有空格,则字符串将为:
"Hello
I like coffee"
Run Code Online (Sandbox Code Playgroud)
"Hello
I like coffee"
Run Code Online (Sandbox Code Playgroud) 试图从一个字段中删除一个不间断的空间,但没有任何成功。我试过了
execute <<-SQL
UPDATE customers
SET name = TRIM (name)
SQL
Run Code Online (Sandbox Code Playgroud)
但这只会删除空格。这是与this类似的问题,但我需要它用于Postgres(Postgres抱怨syntax error at or near "x00A0")并且我只需要修剪即它必须仅在文本的开头和结尾删除。
谢谢
我看到了这个SO答案,它显示了如何删除Bash中字符串中某个字符之后的所有文本。有了这些信息,我可以执行以下操作来舍入一个数字:
NUMBER=10.12345
NUMBER=${NUMBER%.*} # 10
Run Code Online (Sandbox Code Playgroud)
但是,我想保留小数点后两位。我怎么能10.12345成为10.12?我不需要适当舍入,只需修剪即可。也许在运行时带有通配符${NUMBER%.*}?
我想删除所有字符,如逗号、句点、引号等,这样一行:
婴儿汉斯·帕特里克以通常的方式接受了他的乳头膏,而不是通过专利瓶的工具。当他还是个孩子的时候,他的反复无常之一就是当他受到父母严厉的惩罚时,用他的小肺用尽全力尖叫。这种独特的习惯不过是使他在成熟中如此杰出的天才的预示。
...将转换为以下内容:
The infant Hans Patrick received his mammarial balm in the usual way and not through the instrumentality of a patent bottle One of his caprices when yet a child was to scream with all the force of his little lungs when he was severely chastised by his parents This singular habit was but a foreshadowing of that genius which has rendered him so eminent in his maturity
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我可以在空格处拆分单个单词,并且单词末尾没有标点符号。
我正在尝试使用以下代码来做到这一点:
Regex onlyAlphanumericSpaceApostropheAndHyphen = new Regex("[^a-zA-Z0-9 '-]");
. . …Run Code Online (Sandbox Code Playgroud)