我想用给定的输出拆分下面的字符串。 任何人都可以帮我这么做。
实施例:
输出:/ Test /
输出:/ Test1 /
输出:/ Text /
输出:/ 121212121 /
输出:12345
我尝试过string.split函数,但效果不佳。我是否有任何想法或逻辑可以实现这种情况。 如果正则表达式中的答案对我来说没问题。
答案 0 :(得分:6)
您只需要按/
string output = input.Split('/')[0];
但是如果您有//TEST/
并且输出应为/TEST
,则可以使用正则表达式。
string output = Regex.Matches(input, @"\/?(.+?)\/")[0].Groups[1].Value;
对于你的第五种情况:你必须分开逻辑。例如:
public static string Method(string input)
{
var split = input.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if (split.Length == 0) return input;
return split[0];
}
或使用正则表达式。
public static string Method(string input)
{
var matches = Regex.Matches(input, @"\/?(.+?)\/");
if (matches.Count == 0) return input;
return matches[0].Groups[1].Value;
}
使用方法的一些结果:
TEST/54/ => TEST
TEST => TEST
/TEST/ => TEST
答案 1 :(得分:4)
我认为这样可行:
string s1 = "/TEST/TEST123";
string s2 = "/TEST1/Test/Test/Test/";
string s3 = "/Text/12121/1212/";
string s4 = "/121212121/asdfasdf/";
string s5 = "12345";
string pattern = @"\/?[a-zA-Z0-9]+\/?";
Console.WriteLine(Regex.Matches(s1, pattern)[0]);
Console.WriteLine(Regex.Matches(s2, pattern)[0]);
Console.WriteLine(Regex.Matches(s3, pattern)[0]);
Console.WriteLine(Regex.Matches(s4, pattern)[0]);
Console.WriteLine(Regex.Matches(s5, pattern)[0]);
答案 2 :(得分:2)
class Program
{
static void Main(string[] args)
{
string example = "/TEST/TEST123";
var result = GetFirstItem(example);
Console.WriteLine("First in the list : {0}", result);
}
static string GetFirstItem(string value)
{
var collection = value?.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var result = collection[0];
return result;
}
}
StringSplitOptions.RemoveEmptyEntries
是一个枚举,它告诉Split
函数当它将字符串拆分成数组时,如果数组中的元素是空字符串,则该函数不应包含空结果中的元素。基本上你希望集合只包含值。
答案 3 :(得分:1)
public string functionName(string input)
{
if(input.Contains('/'))
{
string SplitStr = input.Split('/')[1];
return "/"+SplitStr .Substring(0, 1) +SplitStr.Substring(1).ToLower()+"/"
}
return input;
}
答案 4 :(得分:1)
private void button1_Click(object sender, EventArgs e)
{
string test = @"/Text/12121/1212/";
int first = test.IndexOf("/");
int last = test.Substring(first+1).IndexOf("/");
string finall = test.Substring(first, last+2);
}
我在所有示例中尝试此代码并获得正确的输出。试试这个。
答案 5 :(得分:1)
以下方法可以帮助您。
# --- imports ---
import pygame
import sys
# --- constants - uppercase ---
HEIGHT = 500
WIDTH = 1000
RED = (255, 0, 0)
WHITE = (255,255,255)
BLACK = ( 0, 0, 0)
BLUE = ( 0, 0,255)
GREEN = ( 0,255, 0)
FPS = 30
BADDIE_THICKNESS = 40
DROPPER_Y = 30
DROPPER_X = 500
HERO_Y = 400
# --- classes - CamelCase ----
class Baddie:
'''enemy class''' # docstring
# empty line for readability
def __init__(self, display, x=DROPPER_X, y=DROPPER_Y, color=GREEN):
self.display = display
self.rect = pygame.Rect(x, y, BADDIE_THICKNESS, BADDIE_THICKNESS)
self.color = color
#self.change = [0, 0]
# empty line for readability
def render(self):
pygame.draw.rect(self.display, self.color, self.rect)
def update(self):
self.rect.y += 1
if self.rect.y > 500:
self.rect.y = 30
class Goodie:
'''player class'''
def __init__(self, display, x=0, y=HERO_Y, color=BLUE):
self.display = display
self.rect = pygame.Rect(x, y, 40, 40)
self.color = color
self.change = 0
def render(self):
pygame.draw.rect(self.display, self.color, self.rect)
def update(self):
self.rect.x += self.change
# stay on screen
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > WIDTH:
self.rect.right = WIDTH
# --- functions - lowercase with _ ---
# ---------------------------------------------------------------------
# --- main ---
# ---------------------------------------------------------------------
class Game:
def __init__(self):
'''--- init ---'''
pygame.init()
self.display = pygame.display.set_mode((WIDTH,HEIGHT))
# create only once
self.good = Goodie(self.display)
self.bads = []
for x in range(1, 11):
self.bads.append(Baddie(self.display, x*90, x*30))
self.font_large = pygame.font.Font(None, 100)
self.game_over_text, self.game_over_rect = self.create_center_text(self.font_large, "GAME OVER")
def create_center_text(self, font, text):
text = font.render(text, True, WHITE)
rect = text.get_rect()
rect.center = self.display.get_rect().center
#rect = text.get_rect(center=display.get_rect().center)
return text, rect
def run(self):
'''--- mainloop ---'''
fps = pygame.time.Clock()
game_over = False
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # exit `gameloop`
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False # exit `gameloop`
elif event.key == pygame.K_LEFT:
self.good.change = -15
elif event.key == pygame.K_RIGHT:
self.good.change = +15
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
self.good.change = 0 # a += 0 is useless because it is a=a+0
# --- updates ---
if not game_over:
# move good
self.good.update()
# move bads
for bad in self.bads:
bad.update()
# check colision
for bad in self.bads:
if self.good.rect.colliderect(bad.rect):
print 'oh no'
game_over = True # exit `gameloop`
# --- draws ---
self.display.fill(BLACK)
for bad in self.bads:
bad.render()
self.good.render()
if game_over:
self.display.blit(self.game_over_text, self.game_over_rect)
pygame.display.update()
# --- FPS ---
fps.tick(FPS)
# --- end ---
pygame.quit()
sys.exit()
#----
Game().run()
答案 6 :(得分:1)
output = (output.Contains("/"))? '/' +input.Split('/')[1]+'/':input;