为什么C#.TrimEnd不能使用@符号?

时间:2016-05-19 21:40:45

标签: c# asp.net

我有一个以 import pygame import os _image_library = {} def get_image(path): global _image_library image = _image_library.get(path) if image == None: canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep) image = pygame.image.load(canonicalized_path) _image_library[path] = image return image pygame.init() screen = pygame.display.set_mode((1000, 800)) done = False clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True screen.fill((0, 0, 0)) screen.blit(get_image('sw.png'), (10, 10)) pygame.display.flip() clock.tick(60) 结尾的字符串,并且在尝试TrimEnd这个字符串时(我只想修剪它,如果@符号实际上是在这个字符串的末尾),我发现C#是完全的无视这一点。

我写了(使用Razor;这是一个Ajax请求)两个隐藏的输入字段,用于在#!/usr/bin/python import pygame from pygame.locals import * pygame.init() pygame.display.set_caption('Title Crawl') screen = pygame.display.set_mode((1000, 800)) screen_r = screen.get_rect() font = pygame.font.SysFont("franklingothicdemibold", 40) clock = pygame.time.Clock() def main(): crawl = ["Star Wars - The Wilds"," ","It is a dark time for the Galaxy. The evil Dark","Lord, Vitiate is rising to power. Alone, a single", "spec is on a trip, a trip that will ultimately", "rectify the wrongs of the galaxy. The keepers ", "of peace are dying out and the DARK SIDE is", "lurking, a conniving force determined to", "become the omniarch."] texts = [] # we render the text once, since it's easier to work with surfaces # also, font rendering is a performance killer for i, line in enumerate(crawl): s = font.render(line, 1, (229, 177, 58)) # we also create a Rect for each Surface. # whenever you use rects with surfaces, it may be a good idea to use sprites instead # we give each rect the correct starting position r = s.get_rect(centerx=screen_r.centerx, y=screen_r.bottom + i * 45) texts.append((r, s)) while True: for e in pygame.event.get(): if e.type == QUIT or e.type == KEYDOWN and e.key == pygame.K_ESCAPE: return screen.fill((0, 0, 0)) for r, s in texts: # now we just move each rect by one pixel each frame r.move_ip(0, -1) # and drawing is as simple as this screen.blit(s, r) # if all rects have left the screen, we exit if not screen_r.collidelistall([r for (r, _) in texts]): return # only call this once so the screen does not flicker pygame.display.flip() # cap framerate at 60 FPS clock.tick(60) if __name__ == '__main__': main() 调用之前和之后保存字符串值,并且值保持不变。

没有空间对这个值进行编码,因为字符串是用C#构建的,并且在我能够证明它不起作用之前不会离开这个环境(最终会进入数据库)。

(变量'NFPA'包含字符串'3-2-0')

@

以下是我从AJAX电话回来后在警报信息中向我朗读的内容。

enter image description here

我会尝试传入字符的ascii编码版本,只是为了看看会发生什么,但是如您所知,.TrimEnd('@')方法只接受char值(而不是字符串)。

我会使用replace,但它必须只在它位于字符串的末尾,并且只有最后一个字符是@符号。

2 个答案:

答案 0 :(得分:7)

TrimEnd是一个返回更改值的函数。实际上,您并没有按原样在代码中分配修剪后的字符串值(返回变量)。

将您的代码更改为:

NFPAFPRACHGLOES = NFPAFPRACHGLOES.TrimEnd('\n')

答案 1 :(得分:5)

string.TrimEnd()不修改字符串,而是创建一个带有反射更改的新字符串。 C#中的字符串是不可变的,因此这是预期的行为,其他字符串方法的工作方式类似。

试试这个:

NFPAFPRACHGLOES = NFPAFPRACHGLOES.TrimEnd('@');