如何删除Java中特定字符之前的所有字符?

时间:2016-12-01 12:32:04

标签: java string replace

我有一个字符串,我通过html表单获取值,所以当我得到它的值时,它会出现在URL中,所以我想删除特定字符=之前的所有字符。我也想删除这个角色。我只想保存=之后的值,因为我需要从变量中获取该值..

编辑:我也需要删除=,因为我试图在字符串后面输入字符/值...

6 个答案:

答案 0 :(得分:19)

您可以使用# Initiate a queue of "raw" input data with embedded Queue Runner. queue = tf.train.string_input_producer(rawdata_filename) # Instantiate Reader Op to read examples from files in the filename queue. reader = tf.FixedLengthRecordReader(record_bytes) # Pull off one instance, decode and cast image and label to 3D, 1D Tensors. result.key, value = reader.read(queue) image_raw, label_raw = decode(value) image = tf.cast(image_raw, dtype) #3D tensor label = tf.cast(label_raw, dtype) #1D tensor # Assume your oversampling factors per class are fixed # and you have 4 classes. OVERSAMPLE_FACTOR = [1,2,4,10] # Now we need to reshape input image tensors to 4D, where the # first dimension is the image number in a batch of oversampled tensors. # images = tf.expand_dims(image, 0) # so, (*,height,width,channels) in 4D # Set up your predicates, which are 1D boolean tensors. # Note you will have to squash the boolean tensors to 0-dimension. # This seems illogical to me, but it is what it is. pred0 = tf.reshape(tf.equal(label, tf.convert_to_tensor([0])), []) #0D tf.bool pred1 = tf.reshape(tf.equal(label, tf.convert_to_tensor([1])), []) #0D tf.bool pred2 = tf.reshape(tf.equal(label, tf.convert_to_tensor([2])), []) #0D tf.bool pred3 = tf.reshape(tf.equal(label, tf.convert_to_tensor([3])), []) #0D tf.bool # Build your callables (functions) that vertically stack an input image and # label tensors X times depending on the accompanying oversample factor. def f0(): return tf.concat(0, [images]*OVERSAMPLE_FACTOR[0]), tf.concat(0, [label]*OVERSAMPLE_FACTOR[0]) def f1(): return tf.concat(0, [images]*OVERSAMPLE_FACTOR[1]), tf.concat(0, [label]*OVERSAMPLE_FACTOR[1]) def f2(): return tf.concat(0, [images]*OVERSAMPLE_FACTOR[2]), tf.concat(0, [label]*OVERSAMPLE_FACTOR[2]) def f3(): return tf.concat(0, [images]*OVERSAMPLE_FACTOR[3]), tf.concat(0, [label]*OVERSAMPLE_FACTOR[3]) # Here we have N conditionals, one for each class. These are exclusive # but due to tf.case() not behaving every conditional gets evaluated. [images, label] = tf.cond(pred0, f0, lambda: [images,label]) [images, label] = tf.cond(pred1, f1, lambda: [images,label]) [images, label] = tf.cond(pred2, f2, lambda: [images,label]) [images, label] = tf.cond(pred3, f3, lambda: [images,label]) # Pass the 4D batch of oversampled tensors to a batching op at the end # of the input data queue. The batching op must be set up to accept # batches of tensors (4D) as opposed to individual tensors (in our case, 3D). images, label_batch = tf.train.batch([images, label], batch_size=batch_size, num_threads=num_threads, capacity=capacity, enqueue_many = True) #accept batches

.substring()

然后String s = "the text=text"; String s1 = s.substring(s.indexOf("=")+1); s1.trim(); 包含原始字符串中s1之后的所有内容。

s1.trim()

=删除字符串(前导空格)的第一个字符(不是空格,如字母,数字等)之前的空格,并删除最后一个字符(尾随空格)后的空格。

答案 1 :(得分:4)

虽然有很多答案。这是一个正则表达式的例子

String test = "eo21jüdjüqw=realString";
test = test.replaceAll(".+=", "");
System.out.println(test);

// prints realString

说明:

.+匹配任何字符(行终止符除外)
+量词 - 在一次和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
=匹配字符=字面(区分大小写)

这也是来自https://regex101.com/的阴影复制粘贴,您可以尝试使用正则表达式。

答案 2 :(得分:1)

您可以从=拆分字符串并分隔到数组中,并将指定的数组的第二个值作为=符号后面的值 例如:

String CurrentString = "Fruit = they taste good"; String[] separated = CurrentString.split("="); separated[0]; // this will contain "Fruit" separated[1]; //this will contain "they teste good"

然后,分隔[1]包含原始字符串中=之后的所有内容。

答案 3 :(得分:1)

可能会在URL字符串中找到第一个出现的字符。例如:

String URL = "http://test.net/demo_form.asp?name1=stringTest";

int index = URL.indexOf("=");

然后,根据索引

拆分String
String Result = URL.substring(index+1); //index+1 to skip =

String Result现在包含值:stringTest

答案 4 :(得分:0)

我知道有人问过Java,但这似乎也是Kotlin的第一个搜索结果,因此您应该知道Kotlin的扩展名为String.substringAfter(delimiter: String, missingDelimiterValue: String = this)情况。

其实现是:

val index = indexOf(delimiter)
return if (index == -1) 
    missingDelimiterValue 
else 
    substring(index + delimiter.length, length)

答案 5 :(得分:0)

如果使用Apache Commons Lang3库,则还可以使用group by实用工具类的substringAfter方法。

官方文档为here

示例:

StringUtils

它管理您的值在第一次出现时就可以包含'='字符的情况。

如果您的键和值也包含'='字符,它将不起作用(但其他方法也是如此);在网址查询参数中,无论如何都应转义此类字符。