sv 去除字符串行尾空格函数

字符串去除行尾空格 函数
// 去除字符串末尾的换行符和空格符
function string remove_trailing_whitespace(string str);
    int len = str.len();
    int last_valid_index = -1;
    
    // 空字符串直接返回
    if (len == 0) return "";
    
    // 从后向前找到第一个非空白字符的位置
    for (int i = len - 1; i >= 0; i--) begin
        byte ch = str[i];
        // 如果不是空格、制表符、换行符、回车符
        if (ch != " " && ch != "\t" && ch != "\n" && ch != "\r") begin
            last_valid_index = i;
            break;
        end
    end
    
    // 如果全是空白字符,返回空字符串
    if (last_valid_index == -1) return "";
    
    // 返回截取后的字符串
    return str.substr(0, last_valid_index);
endfunction
**如果要将function 定义成pub function 可以包含在pkg中 import导入UVM各组件调用,但是function 要定义成automatic function 否则调用会报错**
posted @ 2025-09-28 13:57  shiguang9870  阅读(28)  评论(0)    收藏  举报

© 博客园 · 个人博客

Powered by 博客园  ·  Theme Customized