AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[string](coding)

Martin Hope
Ramanan T
Asked: 2025-04-17 11:29:00 +0800 CST

打印文件中匹配的模式以及匹配的行

  • 7

在一个复杂的脚本中,我使用grep模式文件来获取匹配的行

例如:这是包含文本的文件

$ cat file.txt
abc$(SEQ)asdasd
wwww$(SEQ)asqqqqqq
efg hij$(SEQ)asdasdasd$(SEQ)zzzzzz
klmn$(SEQ)11111111
op$(SEQ)44444444
qrs$(SEQ)777
tuv$(SEQ)mmmmmmmmm
qrs$(SEQ)777444
asdsd777hdhfgjdfasd
wxyzfhdfghdfh

这是模式文件

$ cat pattren.txt
444
777
asd

我正在使用以下grep命令来获取匹配的行

这

在命令行中,我可以看到匹配的模式,但在日志中却看不到。所以我需要一种方法来打印匹配的行和匹配的模式。输出应该类似于:在 TAB 键(或任何可识别的格式)后打印模式。

abc$(SEQ)asdasd <TAB> asd
efg hij$(SEQ)asdasdasd$(SEQ)zzzzzz  <TAB> asd
op$(SEQ)44444444    <TAB>   444
qrs$(SEQ)777    <TAB>   444
qrs$(SEQ)777444  <TAB>  777444
asdsd777hdhfgjdfasd  <TAB>  asd777 

我可以使用 grep-o但无法将两者(即有和无-o)结合在一起。

没有必要使用grep,我很乐意使用任何其他可以完成此操作的命令。

string
  • 3 个回答
  • 58 Views
Martin Hope
Niranjan Wagh
Asked: 2025-03-24 03:18:01 +0800 CST

对于不可变字符串,是否有必要使用 String::from() ?

  • 5

我刚开始学习 Rust。但我不得不问一下,以满足我的好奇心。下面是答案。

我知道 push_str 在两种情况下都会引发错误,因为我没有在任何地方使用mut关键字。

fn main() {
    let my_string1 = "Hello, world! 1";
    let my_string2 = String::from("Hello, world! 2");

    my_string1.push_str("My new String 1");
    my_string2.push_str("My new String 2");

}

但是我在每个教程中都看到人们使用String::from,但他们没有使其可变。如果您的字符串永远不会变异,还有什么理由使用 String::from 吗?我们不能像上面的 my_string1 那样直接用类型 &str 定义它吗?

所以基本上我的意思是问String::from是否只有使用mut关键字才有意义?

string
  • 1 个回答
  • 66 Views
Martin Hope
Tobi
Asked: 2025-02-12 18:11:14 +0800 CST

在 matplotlib 中更改花括号内的字体

  • 5

我试图将图中的字体更改为 Times New Roman,但由于某种原因,x 轴标签中指数的字体没有改变。

我已将全局参数设置如下:

plt.rcParams["font.family"] = "Times New Roman"

除花括号内的部分外,其他都工作正常。

plt.xlabel("Stromdichte A cm$^{-2 test}$", fontsize=40)

x 轴标签

string
  • 1 个回答
  • 31 Views
Martin Hope
zorleone
Asked: 2024-12-01 21:08:00 +0800 CST

不能在无界字符串上使用 `'First` 和 `'Last`

  • 8

当尝试获取无界字符串的第一个或最后一个索引时,如下程序所示

with Ada.Strings; use Ada.Strings; -- for `Backward`
with Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;

procedure Foo is
    package U_Str renames Ada.Strings.Unbounded;

    S : U_Str.Unbounded_String := U_Str.To_Unbounded_String ("example unbounded string");
    I : Natural := U_Str.Index (Source => S,
                                Pattern => "s",
                                Going => Backward,
                                From => S'Last);
begin
    Put_Line (I'Image);
end Foo;

GNAT 抱怨U_Str.Unbounded_String自己是私有类型:

$ gnatmake foo.adb
x86_64-linux-gnu-gcc-10 -c foo.adb
foo.adb:12:41: prefix for "Last" attribute may not be private type
gnatmake: "foo.adb" compilation error

我不明白这一点,同样的程序用常规方法String可以正常工作:

with Ada.Strings; use Ada.Strings; -- for `Backward`
with Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;

procedure Foo is
    package F_Str renames Ada.Strings.Fixed;

    S : String := "example fixed string";
    I : Natural := F_Str.Index (Source => S,
                                Pattern => "s",
                                Going => Backward,
                                From => S'Last);
begin
    Put_Line (I'Image);
end Foo;
$ gnatmake foo.adb 
x86_64-linux-gnu-gcc-10 -c foo.adb
x86_64-linux-gnu-gnatbind-10 -x foo.ali
x86_64-linux-gnu-gnatlink-10 foo.ali
$ ./foo 
 15

无界字符串没有'First和'Last属性吗?Ada 参考手册只说明

下列属性是针对前缀 A 定义的,该前缀 A 属于数组类型(在任何隐式取消引用之后),或表示受约束的数组子类型

但它没有提到任何有关私有类型的信息。这是 GNAT 错误还是我遗漏了什么?

string
  • 1 个回答
  • 40 Views
Martin Hope
Marcel Plch
Asked: 2024-11-24 07:29:10 +0800 CST

Bash 字符串替换——错误?

  • 2

我正在尝试在 bash 中执行一些文件管理,并且我有以下格式的字符串:

1 dir/hello.txt
2 dir2/bar.jpg

当我运行这个替换时,${FOO/[:space:]*/Hello} 我得到了这个结果:1 dir/hHello

目标是将第一个空格后的所有内容(包括空格)替换为Hello

我觉得这似乎是个 bug。在这种情况下,请确认,我会将问题上报。

string
  • 2 个回答
  • 41 Views
Martin Hope
Fatima sami
Asked: 2024-11-17 19:11:12 +0800 CST

为什么我们在计算字符串长度时要切换段?

  • 6

我提供了两种不同的计算字符串长度时切换段的实现。我不明白为什么我们要切换额外段和数据段,或者更准确地说,将数据段(寄存器 DS)的内容复制到额外段(寄存器 ES)。如果我删除这些行push ds,pop es结果不会改变,但是当我les di, [bp+4]用替换时mov di, [bp+4] ,屏幕上会显示消息,但带有一堆其他不同的字符。

为什么会发生这种情况?我是装配方面的新手。

org 0x0100
jmp start

message: db 'Hello world!', 0      ; First string to be concatenated


; Subroutine to calculate the length of a string
; Takes the segment and offset of a string as parameters
strlen:
    push bp
    mov bp, sp
    push es
    push cx
    push di
    les di, [bp+4]        ; Point es:di to string
    mov cx, 0xffff        ; Load maximum number in cx
    xor al, al            ; Load a zero in al
    repne scasb           ; Find zero in the string
    mov ax, 0xffff        ; Load maximum number in ax
    sub ax, cx            ; Find change in cx
    dec ax                ; Exclude null from length
    pop di
    pop cx
    pop es
    pop bp
    ret 4

; Subroutine to print a string
; Takes the x position, y position, attribute, and address of a null-terminated string as parameters
printstr:
    push bp
    mov bp, sp
    pusha
    push di
    push ds               ; Push segment of string
    mov ax, [bp+4]
    push ax               ; Push offset of string
    call strlen           ; Calculate string length

    cmp ax, 0             ; Is the string empty?
    jz exit               ; No printing if string is empty

    mov cx, ax            ; Save length in cx
    mov ax, 0xb800        ; Video memory base address
    mov es, ax            ; Point es to video base
    mov al, 80            ; Load al with columns per row
    mul byte [bp+8]       ; Multiply with y position
    add ax, [bp+10]       ; Add x position
    shl ax, 1             ; Turn into byte offset
    mov di, ax            ; Point di to required location
    mov si, [bp+4]        ; Point si to string
    mov ah, [bp+6]        ; Load attribute in ah
    cld                   ; Clear direction flag for auto-increment mode

 nextchar:
    lodsb                 ; Load next char in al
    stosw                 ; Print char/attribute pair
    loop nextchar         ; Repeat for the whole string

 exit:
    pop ds
    pop di
    popa
    pop bp
    ret 8

start:
    mov ax, 30
    push ax               ; Push x position
    mov ax, 20
    push ax               ; Push y position
    mov ax, 0x7           ; Blue on white attribute
    push ax               ; Push attribute
    mov ax, message
    push ax               ; Push address of message
    call printstr         ; Call the printstr subroutine

    mov ax, 0x4c00        ; Terminate program
    int 0x21

替代版本:

    push bp
    mov bp, sp
    pusha

    push ds
    pop es         ; load ds in es
    mov di, [bp+4] ; point di to string
    mov cx, 0xffff ; load maximum number in cx
    xor al, al 
    repne scasb 
    mov ax, 0xffff 
    sub ax, cx 
    dec ax 
    jz done 

    mov cx, ax
    ...
string
  • 1 个回答
  • 40 Views
Martin Hope
Revive Retro
Asked: 2024-11-16 05:11:22 +0800 CST

如何纠正这些基本错误

  • 6

第一次在这里发帖,我发现 Sol 20 计算机的旧 1977 BASIC 程序中存在一些错误。

第一个错误是“第 4240 行的 DM 错误”:

4220 PRINT "     TYPE IN  -STOP-  TO QUIT ANYTIME"
4230 PRINT
4240 DIM A$(30),B$(30),C$(45),D$(30),E$(30),L$(10),Z$(12)
4250 LET A$="NO SUCH ELEVATION IN MY TAPES, UNDER 360, PLEASE."
4260 PRINT "  THE  TANK IS  ";E;" FEET AWAY."

如果我从第 4240 行删除“B$(30)”,程序将继续运行,但会出现下面的“第 4590 行的 BS 错误”:

4560 IF I<=0 THEN 5250
4570 IF S=0 OR Z=3 THEN 4590
4580 LET S=-S
4590 LET D=(2*X**3*SIN(2*W)*COS(.5*W)/G*4)
4600 LET D=INT(D/15-(3*S-7))
4610 PRINT
4620 PRINT "          ";D;" FEET"

如果我删除“X”后面的一个“*”,程序就可以运行,但范围太小了。熟悉 BASIC 的人可能都能发现错误或提出一些建议。

string
  • 2 个回答
  • 85 Views
Martin Hope
majed tello
Asked: 2024-11-14 01:18:42 +0800 CST

迁移到 .NET 9 后,我在 Select 语句中使用 `string.Join` 时遇到问题

  • 9

以前,当我使用 .NET 8 时,我可以在语句String.Join中使用,如以下代码所示:SelectIQueryable

var receiveTransactions = await repository.Context.Set<ReceiveTransaction>()
    .Select(x => new ThirdPartyTransactionReportModel
    {
        Date = x.Created,
        Customer = string.Join(" - ", x.CustomerIdentity.Customer.Name, x.CustomerIdentity.Customer.Type, x.CustomerIdentity.Customer.Nationality.Name),
    })
    .ToListAsync();

但是,升级到 .NET 9 后,我遇到一个错误:

表达式树不能包含 ref struct 或受限类型“ReadOnlySpan”的值。

错误

请提供任何解决方案或替代方案来解决此问题。

string
  • 1 个回答
  • 52 Views
Martin Hope
Martin
Asked: 2024-11-12 18:29:37 +0800 CST

Rust 的内置 cmp for &str 使用什么排序顺序?

  • 9

正如我从这个问题中理解的那样,有很多种方法可以解释两个字符串的顺序。

OrdRust 为for提供了默认实现str,但我找不到这些问题的答案:

  • 它是按字符排序,还是按原始字节值排序?
  • 如果按字符排序,它使用哪种语言环境?
string
  • 1 个回答
  • 58 Views
Martin Hope
UniqueTurbo
Asked: 2024-11-10 13:03:01 +0800 CST

PS:添加内容获取日期并连接变量

  • 5

我有一个名为 BB_NP 的变量,我想将该内容(字符串)添加到带有日期/时间的输出文本文件中,所以只是一个基本的日志文件。

添加内容-路径“。\LogFile.txt”-值$(Get-Date) ($BB_NP)

每次我尝试时,无论采用何种组合(包装或不包装等),它都只能执行一项,而不能同时执行两项,我相信这是由于两种不同类型的数据造成的。

我怎样才能将其写入文本文件、日期/时间,然后连接 BB_NP 变量的内容?

谢谢!。

string
  • 1 个回答
  • 20 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve